PriorityBlockingQueue in Java

PriorityBlockingQueue in Java is an implementation of BlockingQueue interface and is part of java.util.concurrent package.

PriorityBlockingQueue is an unbounded queue

PriorityBlockingQueue is logically unbounded that’s how is differ from ArrayBlockingQueue which is a bounded queue and LinkedBlockingQueue which is optionally-bounded.

PriorityBlockingQueue though unbounded is created with an initial capacity which you can specify or defaults to 11 if not specified. This initial capacity governs the size of an array used to store the elements on the queue.

Note that though the queue is logically unbounded, attempted additions may fail due to resource exhaustion (causing OutOfMemoryError).

PriorityBlockingQueue is thread-safe

PriorityBlockingQueue is a thread safe variant of java.util.PriorityQueue class and supplies blocking retrieval operations. Insertion methods will never block as the queue is unbounded.

PriorityBlockingQueue does not allow null

PriorityBlockingQueue in Java does not allow null elements. It throws NullPointerException on attempts to add, put or offer a null.

Output

PriorityBlockingQueue is an ordered Queue

The elements of the PriorityBlockingQueue are ordered according to their natural ordering, or by a Comparator provided at queue construction time, depending on which constructor is used. By ordering the element as per your own comparison logic you can prioritize the elements in the priority queue. A priority queue relying on natural ordering also does not permit insertion of non-comparable objects (doing so results in ClassCastException).

The head of this queue is the least element with respect to the specified ordering. The queue retrieval operations poll, remove, peek, and element access the element at the head of the queue.

If multiple elements are having the equal priority, the head is one of those elements i.e. ties are broken arbitrarily.

For example-

Output

As you can see no Comparator is specified here so natural ordering for the Integers (ascending) is used to order the elements in PriorityBlockingQueue.

PriorityBlockingQueue with Comparator

Here is another example where a Comparator is specified for ordering the elements (descending order) in PriorityBlockingQueue.

Output

Java PriorityBlockingQueue Constructors

  • PriorityBlockingQueue()– Creates a PriorityBlockingQueue with the default initial capacity (11) that orders its elements according to their natural ordering.
  • PriorityBlockingQueue(int initialCapacity)– Creates a PriorityBlockingQueue with the specified initial capacity that orders its elements according to their natural ordering.
  • PriorityBlockingQueue(int initialCapacity, Comparator<? super E> comparator)– Creates a PriorityBlockingQueue with the specified initial capacity that orders its elements according to the specified comparator.
  • PriorityBlockingQueue(Collection<? extends E> c)– Creates a PriorityBlockingQueue containing the elements in the specified collection.

PriorityBlockingQueue Java example

BlockingQueue implementations are designed to be used primarily for producer-consumer queues so let’s see an example of producer-consumer using PriorityBlockingQueue. In the example two threads are created one a producer thread and another a consumer thread.

Output

That’s all for the topic PriorityBlockingQueue in Java. If something is missing or you have something to share about the topic please write a comment.


You may also like

  • ReentrantReadWriteLock in Java
  • How to Synchronize HashSet in Java
  • Polymorphism in Java – OOPS Concepts
  • Exception Handling Best Practices in Java
  • How to Convert String to int in Java
  • NameNode, Secondary Namenode and Datanode in HDFS
  • Leave a Reply

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

    This site uses Akismet to reduce spam. Learn how your comment data is processed.