VOOZH about

URL: https://www.javacodegeeks.com/2019/05/priorityblockingqueue-java.html

⇱ PriorityBlockingQueue In Java - Java Code Geeks


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:

//creates an empty PriorityBlockingQueue with capacity of 11
PriorityBlockingQueue()
 
//creates a PriorityBlockingQueue containing elements of c
PriorityBlockingQueue(Collection c)
 
//uses given initialCapacity to instantiate a PriorityBlockingQueue
PriorityBlockingQueue(int initialCapacity)
 
/*creates an empty PriorityBlockingQueue with given initialCapacity
* which orders its elements using given comparator
*/
PriorityBlockingQueue(int initialCapacity, Comparator comparator)

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)
PriorityBlockingQueue<Integer> pbq = new PriorityBlockingQueue<>();
 
pbq.add(4); //Or
pbq.offer(1); //Or
pbq.put(8);

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
    //Assuming pbq has [1, 4, 8] as its elements boolean result = pbq.remove(4); //true System.out.println(pbq); [1, 8]
  • E poll(): The poll() method both retrieves and removes the head element of the queue. It returns null if the queue is empty
    //Assuming pbq has [1, 4, 8] as its elements int polledItem = pbq.poll(); //1 System.out.println(pbq); [4, 8]
  • 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
    //Assuming pbq has [1, 4, 8] as its elements
    int item = pbq.peek(); //1
     
    System.out.println(pbq); [1, 4, 8]
  • 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.

Be the First to comment.

Published on Java Code Geeks with permission by Shubhra Srivastava, partner at our JCG program. See the original article here: PriorityBlockingQueue In Java

Opinions expressed by Java Code Geeks contributors are their own.

Do you want to know how to develop your skillset to become a Java Rockstar?
Subscribe to our newsletter to start Rocking right now!
To get you started we give you our best selling eBooks for FREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to the Terms and Privacy Policy

Thank you!

We will contact you soon.

👁 Photo of Shubhra Srivastava
Shubhra Srivastava
May 20th, 2019Last Updated: May 17th, 2019
1 248 2 minutes read

Shubhra Srivastava

Shubhra is a software professional and founder of ProgrammerGirl. She has a great experience with Java/J2EE technologies and frameworks. She loves the amalgam of programming and coffee :)
Subscribe

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

1 Comment
Oldest
Newest Most Voted
zhou
7 years ago

see you for long time! nice;

1
Reply
Back to top button
Close
wpDiscuz