Core Java

PriorityBlockingQueue In Java

Introduction:

A PriorityBlockingQueue in Java implements the BlockingQueue interface and supports the features of a PriorityQueue. So, what’s a BlockingQueue?

Following holds true for any implementation of a BlockingQueue:

  • While attempting to retrieve an element, a thread waits if the queue is empty
  • In case of a bounded BlockingQueue implementation, the thread waits till it gets space to insert a new element

A PriorityBlockingQueue is an unbounded concurrent blocking queue present in java.util.concurrent package. It doesn’t allow null values.

The elements in a PriorityBlockingQueue are polled based on their priority. By default, an object’s natural ordering is considered to be its priority. We can override the default priority by either implementing Comparable interface or providing a Comparator.

Let’s say we have a PriorityBlockingQueue with two or more elements having the maximum priority. In that case, polling will randomly return one among them.

Creating PriorityBlockingQueue:

We can use one of the following constructors to instantiate a PriorityBlockingQueue:

Commonly Used Methods:

Let’s quickly look at the methods we can use when working with a PriorityBlockingQueue:

1. Insertion:

To insert an element in a PriorityBlockingQueue, we can use any of the following methods:

  • boolean add(E e)
  • boolean offer(E e)
  • void put(E e)

Since a PriorityBlockingQueue is unbounded, all of them behave the same way. In other words, offer(e) will never return false and put(e) will never block the operation.

2. Deletion:

We can remove an element in a PriorityBlockingQueue in one of the following ways:

  • boolean remove(Object obj): This method removes the object obj if it exists and returns true. In case no such element exists, it returns false
  • E poll(): The poll() method both retrieves and removes the head element of the queue. It returns null if the queue is empty
  • E take() throws InterruptedException: Just like poll(), it retrieves and removes the element at the head of the given queue. However, it makes a blocking call i.e. it waits if necessary till an element becomes available

3. Other Methods:

The other commonly used methods include:

  • E peek(): to retrieve and not remove an element from the queue
  • boolean contains(Object obj): To check if the given obj exists in the queue
  • int size(): returns the size of the queue
  • void clear(): removes all the elements of the queue
  • Object[] toArray(): returns an array containing all the elements of the queue in context
  • Comparator comparator(): returns the Comparator used to order the elements of the queue
  • int drainTo(Collection c): removes all available elements in the queue and adds them to the given Collection. It also returns the number of elements transferred
  • int drainTo(Collection c, int maxElements): removes at the most maxElements number of elements from the queue and adds them to the given Collection c

Conclusion:

In this quick tutorial, we explored what a PriorityBlockingQueue is and how to use it. It is a thread-safe implementation and blocks the polling until an element becomes available.

One comment
Pingback: PriorityBlockingQueue In Java | Howdy Android - Android World

Leave a Comment

Your email address will not be published. Required fields are marked *