Java PriorityBlockingQueue Example
November 16, 2023
On this page, I will create PriorityBlockingQueue
example in Java application. It is introduced in Java 5. It belongs to java.util.concurrent
package. PriorityBlockingQueue
implements BlockingQueue
and it also contains the feature of PriorityQueue
.
PriorityBlockingQueue
PriorityBlockingQueue
implements BlockingQueue
and appends the feature of PriorityQueue
. PriorityBlockingQueue
is unbounded. The elements added in it, is ordered on the basis of java Comparable
and Comparator
. By default the elements in PriorityBlockingQueue
are ordered using Comparable
which is implemented by element class. This is also called natural ordering. In case we want to order on the basis of Comparator
, we need to pass its instance while instantiating PriorityBlockingQueue
. Find some method description of PriorityBlockingQueue
. As
add(E e): Adds the given element in the
PriorityBlockingQueue
. Here add(E e), offer(E e) and put(E e) will behave in the same way because PriorityBlockingQueue
is unbounded.
peek():Retrieves the head of the queue if available otherwise null. It does not remove the head.
poll(): Retrieves as well remove the head of queue if available otherwise returns null.
remove(Object o): Removes the specified element.
take(): Retrieves and removes the head of the queue and wait if element is not available.
Example
We will implement a printer job here. There will be two threads, one will add documents and one will print it. While retrieving documents fromPriorityBlockingQueue
, we will observe that elements are retrieved in the order set by Comparable
in our Document
class .
AddDocumentThread.java
package com.concretepage.concurrent; import java.util.concurrent.BlockingQueue; public class AddDocumentThread implements Runnable { BlockingQueue<Document> bqueue; public AddDocumentThread(BlockingQueue<Document> bqueue) { this.bqueue = bqueue; } @Override public void run() { bqueue.offer(new Document(1, "AFSD")); bqueue.offer(new Document(2, "HGTF")); bqueue.offer(new Document(3, "FRDE")); bqueue.offer(new Document(4, "BGTF")); bqueue.offer(new Document(5, "GTGT")); } }
package com.concretepage.concurrent; import java.util.concurrent.BlockingQueue; public class PrintDocumentThread implements Runnable { BlockingQueue<Document> bqueue; public PrintDocumentThread(BlockingQueue<Document> bqueue) { this.bqueue = bqueue; } @Override public void run() { try { for (int i=0; i<5; i++) { Document doc = bqueue.take(); System.out.println(doc.getDocName() +" with id:"+ doc.getDocId()+" printed"); } } catch (InterruptedException e) { e.printStackTrace(); } } }
package com.concretepage.concurrent; public class Document implements Comparable<Document> { private int docId; private String docName; public Document(int bookId, String bookName) { this.docId = bookId; this.docName = bookName; } public int getDocId() { return docId; } public String getDocName() { return docName; } @Override public int compareTo(Document ob) { return docName.compareTo(ob.getDocName()); } }
Run Demo
Main.javapackage com.concretepage.concurrent; import java.util.concurrent.BlockingQueue; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.PriorityBlockingQueue; public class Main { public static void main(String[] args) { final ExecutorService service = Executors.newFixedThreadPool(2); final BlockingQueue<Document> bqueue = new PriorityBlockingQueue<Document>(); service.execute(new AddDocumentThread(bqueue)); service.execute(new PrintDocumentThread(bqueue)); service.shutdown(); } }
AFSD with id:1 printed BGTF with id:4 printed FRDE with id:3 printed GTGT with id:5 printed HGTF with id:2 printed