BlockingQueue offer() method in Java with examples
Last Updated :
04 Jul, 2021
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);
}
}
Outputadding 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);
}
}
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
// 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);
}
}
Output: 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);
}
}
Outputlist 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);
}
}
Output: Exception: java.lang.NullPointerException
Items in Queue are [Karan]
Reference:
Similar Reads
BlockingQueue take() method in Java with examples
The take() method of BlockingQueue interface is used to retrieve and remove the head of this queue. If the queue is empty then it will wait until an element becomes available. This method is more efficient if working on threads and using BlockingQueue in that process. So the thread that initially ca
4 min read
BlockingQueue remove() method in Java with examples
The remove(Object obj) method of BlockingQueue removes only one instance of the given Object, passed as parameter, from this BlockingQueue if it is present. It removes an element e such that obj.equals(e) and if this queue contains one or more instance of element e. This method returns true if this
4 min read
BlockingQueue poll() method in Java with examples
The poll(long timeout, TimeUnit unit) method of BlockingQueue interface returns the head of BlockingQueue by removing that element from the queue. It can be said that this method retrieves and removes element from head of this LinkedBlockingQueue. If queue is empty, then poll() method will wait till
3 min read
BlockingDeque offerLast() method in Java with examples
The offerLast(E e) method of BlockingDeque inserts the element passed in the parameter at the end of the Deque container. If the container's capacity has exceeded, then it does not returns an exception as in case of add() and addLast() function. Syntax: public boolean offerLast(E e) Parameters: This
2 min read
BlockingDeque offerFirst() method in Java with Examples
The offerFirst(E e) method of BlockingDeque inserts the element passed in the parameter at the front of the Deque container. If the container's capacity has exceeded, then it does not returns an exception as in case of add() and addFirst() function. Syntax: public boolean offerFirst(E e) Parameters:
2 min read
BlockingQueue put() method in Java with examples
The put(E e) method of BlockingQueue interface inserts element passed as parameter to method at the tail of this BlockingQueue, if queue is not full. If the queue is full, then this method will wait for space to become available and after space is available, it inserts the element to BlockingQueue.
3 min read
BlockingDeque remove() method in Java with Examples
The remove() method of BlockingDeque removes the head of the Deque container. The method throws a NoSuchElementException if the Deque container is empty. If an element in passed in the parameter, it removes the given element if present in the Deque. Syntax: public E remove() or boolean remove(elemen
2 min read
DelayQueue offer() method in Java with Examples
The offer() method of DelayQueue is used to insert specified element in the delay queue. It acts similar to add() method of DelayQueue.Syntax: public boolean offer (E e) Parameters: DelayQueue accepts only those elements that belong to a class of type Delayed. So, this element E should be of type De
2 min read
BlockingDeque peek() method in Java with examples
The peek() method of BlockingDeque returns the front element in the Deque container. It returns null if the container is empty. Syntax: public E peek() Parameters: This method does not accept any parameters. Returns: This method returns front element in the Deque container if the container is not em
2 min read
BlockingDeque take() method in Java with Examples
The take() method of BlockingDeque returns and removes the head of the Deque container from it. The method throws an InterruptedException if it is interrupted while waiting. Syntax: public E take() Returns: This method returns the head of the Deque container. Exception: The function throws a Interru
2 min read