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
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
{
int capacityOfQueue = 4 ;
BlockingQueue<Integer>
BQ = new LinkedBlockingQueue<Integer>(capacityOfQueue);
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));
System.out.println( "list of numbers of queue:"
+ BQ);
System.out.println( "Empty spaces of queue : "
+ BQ.remainingCapacity());
boolean response = BQ.offer( 2893476 ,
5 ,
TimeUnit.SECONDS);
System.out.println( "Adding new Integer 2893476 is successful: "
+ response);
}
}
|
Output
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
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
{
int capacityOfQueue = 4 ;
BlockingQueue<Integer>
BQ = new LinkedBlockingQueue<Integer>(capacityOfQueue);
System.out.println( "Adding 283239 in Queue :"
+ BQ.offer( 283239 ,
5 ,
TimeUnit.SECONDS));
try {
System.out.println( "Adding null in Queue: "
+ BQ.offer( null ,
5 ,
TimeUnit.SECONDS));
}
catch (Exception e) {
System.out.println( "Exception: " + e);
}
System.out.println( "Items in Queue are "
+ BQ);
}
}
|
Output:
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
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.BlockingQueue;
public class GFG {
public static void main(String[] args)
{
int capacityOfQueue = 4 ;
BlockingQueue<String>
BQ = new LinkedBlockingQueue<String>(capacityOfQueue);
BQ.offer( "dean" );
BQ.offer( "kevin" );
BQ.offer( "sam" );
BQ.offer( "jack" );
System.out.println( "list of names of queue:" );
System.out.println(BQ);
}
}
|
Output:
list of names of queue:
[dean, kevin, sam, jack]
Program 2:
Java
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.BlockingQueue;
public class GFG {
public static void main(String[] args)
{
int capacityOfQueue = 4 ;
BlockingQueue<Integer>
BQ = new LinkedBlockingQueue<Integer>(capacityOfQueue);
BQ.offer( 34567 );
BQ.offer( 45678 );
BQ.offer( 98323 );
BQ.offer( 93758 );
System.out.println( "list of numbers of queue:" );
System.out.println(BQ);
System.out.println( "Empty spaces of queue : "
+ BQ.remainingCapacity());
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);
}
}
|
Output
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
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.BlockingQueue;
public class GFG {
public static void main(String[] args)
throws InterruptedException
{
int capacityOfQueue = 4 ;
BlockingQueue<String> BQ
= new LinkedBlockingQueue<String>(capacityOfQueue);
BQ.offer( "Karan" );
try {
BQ.offer( null );
}
catch (Exception e) {
System.out.println( "Exception: " + e);
}
System.out.println( "Items in Queue are "
+ BQ);
}
}
|
Output:
Exception: java.lang.NullPointerException
Items in Queue are [Karan]
Reference:
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 :
04 Jul, 2021
Like Article
Save Article