There are two types of offer() method for BlockingQueue interface:
Note: The offer() method of BlockingQueue has been inherited from the Queue class in Java.
offer(E e, long timeout, TimeUnit unit)
The offer(E e, long timeout, TimeUnit unit) method of BlockingQueue inserts the element passed as parameter to method at the tail of this BlockingQueue if queue is not full. It will wait till a specified time for space to become available if the BlockingQueue 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 BlockingQueue to remove some elements so that this method can add elements to BlockingQueue.
Syntax:
public boolean offer(E e, long timeout, TimeUnit unit) throws InterruptedException
Parameters: This method accepts three parameters:
- e– the element to be inserted into BlockingQueue.
- timeout– the time till which offer method will wait for inserting new element is queue is full.
- unit– the Time unit for timeout parameter.
Return Value: The method returns true if insertion of element successful. Else it returns false if the specified waiting time elapses before space is available.
Exception: This method throws following exceptions:
- NullPointerException– if the specified element is null.
- InterruptedException– if interrupted while waiting.
Below programs illustrates offer(E e, long timeout, TimeUnit unit) method of BlockingQueue class:
Program 1:
Java
// Java Program Demonstrate // offer(Element e, long timeout, TimeUnit unit) // method of BlockingQueue. import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.BlockingQueue; import java.util.concurrent.TimeUnit; public class GFG { // Main method public static void main(String[] args) throws InterruptedException { // define capacity of BlockingQueue int capacityOfQueue = 4 ; // create object of BlockingQueue BlockingQueue<Integer> BQ = new LinkedBlockingQueue<Integer>(capacityOfQueue); // Add 5 elements to BlockingQueue having // Timeout in seconds with value 5 secs in // offer(Element e, long timeout, TimeUnit unit) System.out.println( "adding 32673821 " + BQ.offer( 32673821 , 5 , TimeUnit.SECONDS)); System.out.println( "adding 88527183: " + BQ.offer( 88527183 , 5 , TimeUnit.SECONDS)); System.out.println( "adding 431278539: " + BQ.offer( 431278539 , 5 , TimeUnit.SECONDS)); System.out.println( "adding 351278693: " + BQ.offer( 351278693 , 5 , TimeUnit.SECONDS)); System.out.println( "adding 647264: " + BQ.offer( 647264 , 5 , TimeUnit.SECONDS)); // print the elements of queue System.out.println( "list of numbers of queue:" + BQ); // now queue is full check remaining capacity of queue System.out.println( "Empty spaces of queue : " + BQ.remainingCapacity()); // try to add more Integer boolean response = BQ.offer( 2893476 , 5 , TimeUnit.SECONDS); System.out.println( "Adding new Integer 2893476 is successful: " + response); } } |
adding 32673821 true adding 88527183: true adding 431278539: true adding 351278693: true adding 647264: false list of numbers of queue:[32673821, 88527183, 431278539, 351278693] Empty spaces of queue : 0 Adding new Integer 2893476 is successful: false
Program 2:
Java
// Java Program Demonstrate // offer(Element e, long timeout, TimeUnit unit) // method of BlockingQueue. import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.BlockingQueue; import java.util.concurrent.TimeUnit; public class GFG { public static void main(String[] args) throws InterruptedException { // define capacity of BlockingQueue int capacityOfQueue = 4 ; // create object of BlockingQueue BlockingQueue<Integer> BQ = new LinkedBlockingQueue<Integer>(capacityOfQueue); // Add elements to BlockingQueue having // Timeout in seconds with value 5 secs in // offer(Element e, long timeout, TimeUnit unit) System.out.println( "Adding 283239 in Queue :" + BQ.offer( 283239 , 5 , TimeUnit.SECONDS)); // try to put null value in offer method try { System.out.println( "Adding null in Queue: " + BQ.offer( null , 5 , TimeUnit.SECONDS)); } catch (Exception e) { // print error details System.out.println( "Exception: " + e); } // print elements of queue System.out.println( "Items in Queue are " + BQ); } } |
Adding 283239 in Queue :true Exception: java.lang.NullPointerException Items in Queue are [283239]
offer(E e)
The offer(E e) method of BlockingQueue inserts the element e, passed as parameter, at the tail of this BlockingQueue, if queue has space i.e Queue is not full. If queue is full then applying offer() method shows no effect because BlockingQueue will blocks element to be inserted. offer() method returns true when the operation of addition to BlockingQueue 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: This method takes a mandatory parameter e which is the element to be inserted into LinkedBlockingQueue.
Return Value: This method returns true if insertion of element successful. Else it returns false.
Exception: The method throws NullPointerException if the specified element is null.
Below programs illustrates offer() method of BlockingQueue class
Program 1:
Java
// Java Program Demonstrate // offer(Element e) // method of BlockingQueue. import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.BlockingQueue; public class GFG { // Main method public static void main(String[] args) { // define capacity of BlockingQueue int capacityOfQueue = 4 ; // create object of BlockingQueue BlockingQueue<String> BQ = new LinkedBlockingQueue<String>(capacityOfQueue); // Add element to BlockingQueue using offer BQ.offer( "dean" ); BQ.offer( "kevin" ); BQ.offer( "sam" ); BQ.offer( "jack" ); // print the elements of queue System.out.println( "list of names of queue:" ); System.out.println(BQ); } } |
list of names of queue: [dean, kevin, sam, jack]
Program 2:
Java
// Java Program Demonstrate // offer(Element e) // method of BlockingQueue. import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.BlockingQueue; public class GFG { // Main method public static void main(String[] args) { // define capacity of BlockingQueue int capacityOfQueue = 4 ; // create object of BlockingQueue BlockingQueue<Integer> BQ = new LinkedBlockingQueue<Integer>(capacityOfQueue); // Add element to BlockingQueue using offer BQ.offer( 34567 ); BQ.offer( 45678 ); BQ.offer( 98323 ); BQ.offer( 93758 ); // print the elements of queue System.out.println( "list of numbers of queue:" ); System.out.println(BQ); // now queue is full check remaining capacity of queue System.out.println( "Empty spaces of queue : " + BQ.remainingCapacity()); // try to add extra Integer boolean response = BQ.offer( 2893476 ); System.out.println( "Adding new Integer 2893476 is successful: " + response); response = BQ.offer( 456751 ); System.out.println( "Adding new Integer 456751 is successful: " + response); } } |
list of numbers of queue: [34567, 45678, 98323, 93758] Empty spaces of queue : 0 Adding new Integer 2893476 is successful: false Adding new Integer 456751 is successful: false
Program 3: Showing Exception thrown by offer() method
Java
// Java Program Demonstrate offer(E e) // method of LinkedBlockingQueue import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.BlockingQueue; public class GFG { public static void main(String[] args) throws InterruptedException { // define capacity of BlockingQueue int capacityOfQueue = 4 ; // create object of BlockingQueue BlockingQueue<String> BQ = new LinkedBlockingQueue<String>(capacityOfQueue); // Add element using offer() method BQ.offer( "Karan" ); // try to put null value in offer method try { BQ.offer( null ); } catch (Exception e) { // print error details System.out.println( "Exception: " + e); } // print elements of queue System.out.println( "Items in Queue are " + BQ); } } |
Exception: java.lang.NullPointerException Items in Queue are [Karan]
Reference:
- https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/BlockingQueue.html#offer(E)
- https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/BlockingQueue.html#offer(E, %20long, %20java.util.concurrent.TimeUnit)