Can I use PriorityBlockingQueue with multiple threads?

java, multithreading, thread-safety

Solution

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.

Problem

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`.

Original source