Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I use PriorityBlockingQueue with multiple threads?

How many locks does PriorityBlockingQueue have? Are the take and put operations synchronized? I couldnt find much information regarding this type of queue. I was using the single threaded PriorityQueue.

like image 300
Hussein Zawawi Avatar asked Nov 15 '12 13:11

Hussein Zawawi


People also ask

Is PriorityBlockingQueue thread-safe?

PriorityBlockingQueue is a thread-safe and blocking variant of the PriorityQueue. In the linked article, you will also learn what a priority queue is. As with PriorityQueue , the elements are stored in an array representing a min-heap. The iterator iterates through the elements in the corresponding order.

What is the difference between PriorityBlockingQueue and a normal BlockingQueue?

It's also worth mentioning that the BlockingQueue interface also provides us with ways of blocking when adding to full queues. However, a PriorityBlockingQueue is unbounded. This means that it will never be full, thus it will always possible to add new elements.

Is PriorityQueue is thread-safe?

Since PriorityQueue is not thread-safe, java provides PriorityBlockingQueue class that implements the BlockingQueue interface to use in a java multithreading environment.

Is multi threaded thread-safe?

So, it's considered to be thread-safe and can be safely called by multiple threads at the same time. All threads can safely call the factorial() method and will get the expected result without interfering with each other and without altering the output that the method generates for other threads.


2 Answers

How many locks PriorityBlockingQueue have?

That is an implementation detail that does not matter. Unless you want to understand how it is implemented in which case I can only suggest that you looked at the source code.

take and put operations are synchronized?

They are probably not synchronized strictly speaking, but the class is thread safe so you can take and put simultaneously in several threads.

Note: the javadoc of PriorityBlockingQueue is not very explicit on that point, but if you look at the javadoc of the java.util.concurrent package, you will see:

Five implementations in java.util.concurrent support the extended BlockingQueue interface, that defines blocking versions of put and take: LinkedBlockingQueue, ArrayBlockingQueue, SynchronousQueue, PriorityBlockingQueue, and DelayQueue.

And BlockingQueue clearly states:

BlockingQueue implementations are thread-safe. All queuing methods achieve their effects atomically using internal locks or other forms of concurrency control. However, the bulk Collection operations addAll, containsAll, retainAll and removeAll are not necessarily performed atomically unless specified otherwise in an implementation.

like image 153
assylias Avatar answered Sep 24 '22 18:09

assylias


From reading the HotSpot Java 7 source code there is only one lock, called lock.

Different implementations are possible as this is not a documented requirement of the class.

like image 38
Peter Lawrey Avatar answered Sep 22 '22 18:09

Peter Lawrey