Open In App

PriorityBlockingQueue offer() method in Java

Improve
Improve
Like Article
Like
Save
Share
Report

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




// Java Program Demonstrate offer()
// method of PriorityBlockingQueue
 
import java.util.concurrent.PriorityBlockingQueue;
 
public class GFG {
    public static void main(String[] args)
    {
 
        // define capacity of PriorityBlockingQueue
        int capacityOfQueue = 5;
 
        // create object of PriorityBlockingQueue
        PriorityBlockingQueue<Integer> PrioQueue
            = new PriorityBlockingQueue<Integer>(capacityOfQueue);
 
        // Add numbers to PriorityBlockingQueue using offer()
        PrioQueue.offer(35658786);
        PrioQueue.offer(5278367);
        PrioQueue.offer(74381793);
        PrioQueue.offer(87625142);
 
        // print elements of PriorityBlockingQueue
        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




// Java Program Demonstrate offer()
// method of PriorityBlockingQueue
 
import java.util.concurrent.PriorityBlockingQueue;
import java.util.*;
 
public class GFG {
    public static void main(String[] args)
    {
 
        // define capacity of PriorityBlockingQueue
        int capacityOfQueue = 5;
 
        // create object of PriorityBlockingQueue which contains
        // name of students
        PriorityBlockingQueue<String> names
            = new PriorityBlockingQueue<String>(capacityOfQueue);
 
        // Add names of students of girls college
        names.offer("Joyita");
        names.offer("Priyanka");
 
        // try to insert null value in offer method
        try {
            names.offer(null);
        }
        catch (Exception e) {
            // print error details
            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

  1. e : the element to be inserted in PriorityBlockingQueue.
  2. timeout : this parameter is neglected because queue never blocks new element insertion.
  3. 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




// Java Program Demonstrate offer()
// method of PriorityBlockingQueue
 
import java.util.concurrent.PriorityBlockingQueue;
import java.util.concurrent.TimeUnit;
 
public class GFG {
    public static void main(String[] args)
    {
 
        // define capacity of PriorityBlockingQueue
        int capacityOfQueue = 5;
 
        // create object of PriorityBlockingQueue
        PriorityBlockingQueue<Integer> PrioQueue
            = new PriorityBlockingQueue<Integer>(capacityOfQueue);
 
        // Add 3 elements to PriorityBlockingQueue using
        // offer(Element e, long timeout, TimeUnit unit)
        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));
 
        // print elements of PriorityBlockingQueue
        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:



Last Updated : 29 Jun, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads