Open In App
Related Articles

LinkedTransferQueue offer() method in Java

Improve Article
Improve
Save Article
Save
Like Article
Like

offer(E e, long timeout, TimeUnit unit)

The offer(E e, long timeout, TimeUnit unit) method of java.util.concurrent.LinkedTransferQueue Class is an in-built function in Java which inserts the element passed as parameter to method at the tail of this queue, if queue is not full.

  • It will wait till a specified time for space to become available if the LinkedTransferQueue is full.
  • The specified waiting time and TimeUnit for time will be given as parameters to the offer method so it will wait till that time for LinkedTransferQueue to remove some elements so offer method can add elements to LinkedTransferQueue.

Syntax:

public boolean offer(E e, long timeout, TimeUnit unit)

Parameters: The function accepts the following parameters:

  • e– the element to be inserted.
  • timeout
  • unit– a TimeUnit determining how to interpret the timeout parameter

Return Value: The method returns a boolean true. As the queue is unbounded, this method will never block or return false.

Exception: The function shows NullPointerException when the specified element is Null.

Below programs illustrate the use of java.util.concurrent.LinkedTransferQueue.offer() :

Program 1: To create LinkedTransferQueue by inserting the names of students using offer(E e, long timeout, TimeUnit unit) method whose timeunit parameter is given in seconds and the timeout parameter is 5sec




// Java Program Demonstrate offer()
// method of LinkedTransferQueue
  
import java.util.concurrent.LinkedTransferQueue;
  
class GFG {
    public static void main(String[] args)
        throws InterruptedException
    {
  
        // Initializing the queue
        LinkedTransferQueue<Integer>
            queue = new LinkedTransferQueue<Integer>();
  
        // add elements to queue
        queue.offer(10);
        queue.offer(20);
        queue.offer(30);
  
        try {
            queue.offer(null);
        }
        catch (Exception e) {
            System.out.println("Exception: " + e);
        }
    }
}


Output:

adding 15 true
adding 25: true
adding 35: true
adding 45: true
adding 55: true
list of numbers of queue: [15, 25, 35, 45, 55]

Program 2: Program to show NullPointerException.





Output:

Exception: java.lang.NullPointerException

offer(E e)

The offer(E e) method of java.util.concurrent.LinkedTransferQueue Class is an in-built function in Java which inserts the element e passed as parameter to method at the tail of this LinkedTransferQueue if queue has space i.e Queue is not full. If queue is full then applying offer() method shows no effect because LinkedTransferQueue will blocks element to be inserted. offer() method returns true when the operation of addition to LinkedTransferQueue is successful and false if this queue is full. This method is preferred over add() method because add method throws error when queue is full but offer() method returns false in such situation.

Syntax:

public boolean offer(E e)

Parameters: The function accepts a single parameter e i.e. the element to be inserted.

Return Value: The method returns true upon successful insertion.

Exception: The function shows NullPointerException when the specified element is Null.

Program 1: Adding String in the queue.




// Java Program Demonstrate offer()
// method of LinkedTransferQueue
  
import java.util.concurrent.*;
  
class GFG {
    public static void main(String[] args)
    {
  
        // Initializing the queue
        LinkedTransferQueue<String>
            queue = new LinkedTransferQueue<String>();
  
        // Adding elements to this queue
        queue.offer("alex");
        queue.offer("bob");
        queue.offer("chuck");
        queue.offer("drake");
        queue.offer("eric");
  
        // Printing the elements of the queue
        System.out.print("Queue: ");
        for (String i : queue)
            System.out.print(i + " ");
    }
}


Output:

Queue: alex bob chuck drake eric

Program 2: Program to show NullPointerException.




// Java Program Demonstrate offer()
// method of LinkedTransferQueue
  
import java.util.concurrent.LinkedTransferQueue;
  
class GFG {
    public static void main(String[] args)
        throws InterruptedException
    {
  
        // Initializing the queue
        LinkedTransferQueue<Integer>
            queue = new LinkedTransferQueue<Integer>();
  
        // add elements to queue
        queue.offer(10);
        queue.offer(20);
        queue.offer(30);
  
        try {
            queue.offer(null);
        }
        catch (Exception e) {
            System.out.println("Exception: " + e);
        }
    }
}


Output:

Exception: java.lang.NullPointerException

Reference: https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/LinkedTransferQueue.html#offer(E)
https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/LinkedTransferQueue.html#offer(E, %20long, %20java.util.concurrent.TimeUnit)


Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 26 Nov, 2018
Like Article
Save Article
Previous
Next
Similar Reads
Complete Tutorials