Java Program to Implement PriorityBlockingQueue API Last Updated : 20 Oct, 2022 Summarize Comments Improve Suggest changes Share Like Article Like Report 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 collectionjava.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 returned in // PriorityBlockingQueue System.out.println("Polled : " + poll); // Pause the execution of current 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 occurred e.printStackTrace(); } // Execution of thread begins with // use of start() method }).start(); // Custom elements inputs // 1, 2, 3 to priorityBlockingQueue // Pausing execution 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: Comment More infoAdvertise with us Next Article Java Program to Implement PriorityBlockingQueue API M mayanktyagi1709 Follow Improve Article Tags : Java Technical Scripter Java Programs Technical Scripter 2020 Java-Collections Java-PriorityBlockingQueue +2 More Practice Tags : JavaJava-Collections Similar Reads Java Program to Implement PriorityQueue API A PriorityQueue is a linear data structure in which the elements are ordered according to their natural ordering or by some custom comparator provided at the queue at construction time. In PriorityQueue, the front of the queue points to the least element, and the rear points to the greatest element 4 min read Java Program to Implement LinkedBlockingQueue API LinkedBlockingQueue API is an optionally-bounded queue based on linked nodes. It orders the elements in FIFO(First In First Out) order. The head of this queue is the element that has been there in the queue for the longest time and the tail of the queue is the element that has been in the queue for 6 min read Java Program to Implement LinkedTransferQueue API LinkedTransferQueue is a queue that orders elements FIFO (first-in-first-out) with respect to any given producer. The head of the queue is that element that has been on the queue the longest time for some producer. The tail of the queue is that element that has been on the queue the shortest time fo 5 min read Java Program to Implement SynchronousQueue API SynchronousQueue is a special blocking queue with no internal capacity. It helps in exchange data or information between threads in a thread-safe manner. SynchronousQueue has only 2 supported operations: Both of these are blocking method which means when we want to add a piece of information or data 3 min read Java Program to Implement ArrayBlockingQueue API ArrayBlockingQueue class is a member of the Java Collection framework. ArrayBlockingQueue is a bounded blocking queue. The term bounded, means that the size of the Queue is fixed and cannot be changed. Any attempt to put element/elements into a full queue will lead to blocking operation. Similarly, 7 min read Like