Skip to content
Related Articles

Related Articles

Improve Article

LinkedTransferQueue offer() method in Java

  • Last Updated : 26 Nov, 2018

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.*;
  
class GFG {
    public static void main(String[] args)
        throws InterruptedException
    {
  
        // create object of LinkedTransferQueue
        LinkedTransferQueue<Integer>
            queue = new LinkedTransferQueue<Integer>();
  
        // Add 5 elements to ArrayBlockingQueue having
        // Timeout in seconds with value 5 secs in
        // offer(Element e, long timeout, TimeUnit unit)
  
        System.out.println("adding 15 "
                           + queue.offer(15, 5, TimeUnit.SECONDS));
        System.out.println("adding 25: "
                           + queue.offer(25, 5, TimeUnit.SECONDS));
        System.out.println("adding 35: "
                           + queue.offer(35, 5, TimeUnit.SECONDS));
        System.out.println("adding 45: "
                           + queue.offer(45, 5, TimeUnit.SECONDS));
        System.out.println("adding 55: "
                           + queue.offer(55, 5, TimeUnit.SECONDS));
  
        // print the elements of queue
        System.out.println("list of numbers of queue: "
                           + queue);
    }
}
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.




// Java Program Demonstrate offer()
// method of LinkedTransferQueue
  
import java.util.concurrent.*;
  
class GFG {
    public static void main(String[] args)
        throws InterruptedException
    {
  
        // Initializing the queue
        LinkedTransferQueue<Integer>
            queue = new LinkedTransferQueue<Integer>();
  
        // add elements to queue
        try {
            queue.offer(null, 5, TimeUnit.SECONDS);
        }
        catch (Exception e) {
            System.out.println("Exception: " + e);
        }
    }
}
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)

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :