Skip to content
Related Articles

Related Articles

Java Program to Implement PriorityBlockingQueue API
  • Last Updated : 28 Jan, 2021

PriorityBlockingQueue is an unbounded blocking queue that uses the same ordering rules as class PriorityQueue and supplies blocking retrieval operations. The “blocking” part of the name is added to imply the thread will block waiting until there’s an item available on the queue. This class does not permit null elements. A priority queue relying on natural ordering also does not permit the insertion of non-comparable objects (doing so results in ClassCastException).

It implements Serializable, Iterable<E>, Collection<E>, BlockingQueue<E>, Queue<E> interfaces and extends AbstractQueue<E> class.

Declaration:

public class PriorityBlockingQueue<E> extends AbstractQueue<E> implements BlockingQueue<E>, Serializable
// Here, E is the type of elements held in this collection
java.lang.Object
   java.util.AbstractCollection<E>
       java.util.AbstractQueue<E>
          java.util.concurrent.PriorityBlockingQueue<E>

Implementation: 



Example

Java




// Java Program to Implement PriorityBlockingQueue API
  
// Importing concurrent classes from
// java.util package
import java.util.concurrent.PriorityBlockingQueue;
import java.util.concurrent.TimeUnit;
  
// Class for PriorityQueue
public class GFG {
    // Main driver method
    public static void main(String[] args)
        throws InterruptedException
    {
        // Creating a new object of PriorityBlockingQueue
        // Declaring Integer type object
        PriorityBlockingQueue<Integer> priorityBlockingQueue
            = new PriorityBlockingQueue<>();
  
        // Creation of a thread
        new Thread(() -> {
            // Display message
            System.out.println("Waiting to poll ...");
  
            // Try block to check for exceptions
            try {
  
                // Condition check
                while (true) {
  
                    // Return (integer) value at head of
                    // queue of PriorityBlockingQueue
                    Integer poll
                        = priorityBlockingQueue.take();
  
                    // Display and print element returnd in
                    // PriorityBlockingQueue
                    System.out.println("Polled : " + poll);
  
                    // Pause the execution of curent thread
                    // for certain amount of time using
                    // toMills() method() to showcase
                    // working of PriorityBlockingQueue
                    Thread.sleep(
                        TimeUnit.SECONDS.toMillis(1));
                }
            }
  
            // Catch block to handle exceptions if any
            catch (InterruptedException e) {
  
                // Print and display the line number
                // where exception/s occured
                e.printStackTrace();
            }
  
            // Execution of thread begins with
            // use of start() method
        }).start();
  
        // Custom elememts inputs
        // 1, 2, 3 to priorityBlockingQueue
  
        // Pausing executio of first thread
        Thread.sleep(TimeUnit.SECONDS.toMillis(2));
  
        // Insert parameter element-> 1 to method
        // at the tail of priority queue
        priorityBlockingQueue.add(1);
  
        // Pausing execution of second thread
        Thread.sleep(TimeUnit.SECONDS.toMillis(2));
  
        // Insert parameter element-> 2 to method
        // at the tail of priority queue
        priorityBlockingQueue.add(2);
  
        // pausing execution of third thread
        Thread.sleep(TimeUnit.SECONDS.toMillis(2));
  
        // Insert parameter element-> 3 to method
        // at the tail of priority queue
        priorityBlockingQueue.add(3);
    }
}

Output:

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :