Home  >  Core Java

Example of PriorityBlockingQueue in Java

By Arvind Rai, December 09, 2012
On this page we will provide example of PriorityBlockingQueue in java. PriorityBlockingQueue has been introduced in JDK 1.5. It belongs to java.util.concurrent package. PriorityBlockingQueue implements BlockingQueue and it also contains the feature of PriorityQueue. Here on this page we will provide description and usability of PriorityBlockingQueue.

BlockingQueue

BlockingQueue is a Queue but with some different features. While retrieving element from BlockingQueue, thread waits if it is empty. Once the element is added in BlockingQueue, threads is unblocked retrieving element successfully. Same happens when thread is trying to add element in BlockingQueue. If the size of BlockingQueue is limited and it is already full, then thread waits until it gets space to add element.

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.

PriorityBlockingQueue 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 from PriorityBlockingQueue, 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"));			
	}
} 
PrintDocumentThread.java
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();
		}
	}
} 
Document.java
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.java
package 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();
	}
} 
Find the output.
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 

Download Source Code

POSTED BY
ARVIND RAI
ARVIND RAI
FIND MORE TUTORILAS






©2019 concretepage.com | Privacy Policy | Contact Us