1. offer(E e) Method
The offer(E e) method of PriorityBlockingQueue inserts the element e, passed as parameter, in this PriorityBlockingQueue.This method inserts the element into this PriorityBlockingQueue. Since the PriorityBlockingQueue is unbounded, this method will be never be blocked. Syntax:
public boolean offer(E e)
Parameter: This method accepts one parameter e which represents the element e we want to insert in this PriorityBlockingQueue. Returns: This method returns boolean response true if insertion is successful. Exception: This method throws following Exceptions.
- ClassCastException – if the specified element cannot be compared with elements currently in the priority queue according to the PriorityBlockingQueue’s ordering.
- NullPointerException– if the element is null
Below programs illustrate offer() method of PriorityBlockingQueue: Example 1: Program to demonstrate offer() method on PriorityBlockingQueue to add a list of numbers.
Java
import java.util.concurrent.PriorityBlockingQueue;
public class GFG {
public static void main(String[] args)
{
int capacityOfQueue = 5 ;
PriorityBlockingQueue<Integer> PrioQueue
= new PriorityBlockingQueue<Integer>(capacityOfQueue);
PrioQueue.offer( 35658786 );
PrioQueue.offer( 5278367 );
PrioQueue.offer( 74381793 );
PrioQueue.offer( 87625142 );
System.out.println("Queue Contains:");
System.out.println(PrioQueue.toString());
}
}
|
Output:Queue Contains:
[5278367, 35658786, 74381793, 87625142]
Example 2: Program to demonstrate Exception thrown by offer() method when we try to add null to PriorityBlockingQueue.
Java
import java.util.concurrent.PriorityBlockingQueue;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
int capacityOfQueue = 5 ;
PriorityBlockingQueue<String> names
= new PriorityBlockingQueue<String>(capacityOfQueue);
names.offer("Joyita");
names.offer("Priyanka");
try {
names.offer( null );
}
catch (Exception e) {
System.out.println("Exception Thrown: " + e);
}
}
}
|
Output:Exception Thrown: java.lang.NullPointerException
2. offer(E e, long timeout, TimeUnit unit) Method
The offer(E e, long timeout, TimeUnit unit) method of PriorityBlockingQueue inserts the element e, passed as parameter, in this PriorityBlockingQueue.This method inserts the element into this PriorityBlockingQueue. Since the PriorityBlockingQueue is unbounded, this method will be never be blocked and timeout and timeUnit parameters are neglected.This method is inherited from superclass of priorityBlockingQueue which is BlockingQueue class But as priorityBlockingQueue never blocks new element to be inserted so this method inherited from super class is logically same as offer(E e) method. Syntax:
offer(E e, long timeout, TimeUnit unit)
Parameter: This method accepts three parameters
- e : the element to be inserted in PriorityBlockingQueue.
- timeout : this parameter is neglected because queue never blocks new element insertion.
- unit : this parameter is also neglected because queue never blocks new element insertion.
Returns: This method returns boolean response true if insertion is successful. Exception: This method throws following Exceptions.
- ClassCastException – if the specified element cannot be compared with elements currently in the priority queue according to the PriorityBlockingQueue’s ordering.
- NullPointerException– if the element is null
Below programs illustrate offer(E e, long timeout, TimeUnit unit) method of PriorityBlockingQueue: Example 1: Program to demonstrate offer(E e, long timeout, TimeUnit unit) method on PriorityBlockingQueue to add a list of numbers.
Java
import java.util.concurrent.PriorityBlockingQueue;
import java.util.concurrent.TimeUnit;
public class GFG {
public static void main(String[] args)
{
int capacityOfQueue = 5 ;
PriorityBlockingQueue<Integer> PrioQueue
= new PriorityBlockingQueue<Integer>(capacityOfQueue);
System.out.println("adding 1234 "
+ PrioQueue.offer( 1234 ,
5 , TimeUnit.SECONDS));
System.out.println("adding 2345 "
+ PrioQueue.offer( 2345 ,
5 , TimeUnit.SECONDS));
System.out.println("adding 3456 "
+ PrioQueue.offer( 3456 ,
5 , TimeUnit.SECONDS));
System.out.println("Queue Contains:");
System.out.println(PrioQueue.toString());
}
}
|
Output:adding 1234 true
adding 2345 true
adding 3456 true
Queue Contains:
[1234, 2345, 3456]
Reference: