9

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.

4
  • docs.oracle.com/javase/6/docs/api/java/util/concurrent/… Commented Nov 15, 2012 at 13:18
  • 2
    From the BlockingQueue javadocs it says "BlockingQueue implementations are thread-safe". Commented Nov 15, 2012 at 13:18
  • Looking at the actual implementation, it uses a java.util.concurrent.locks.ReentrantLock for thread safety. This is true for almost all its methods. Commented Nov 15, 2012 at 13:21
  • Whatever implementation is used, if it's advertised as thread-safe, then it should behave as if take and put are synchronised. Which means that if we think of it as having a single lock, it's a good mental model. There won't be any unexpected surprises. Commented Jun 27, 2015 at 11:51

3 Answers 3

20

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

0

From the Javadoc for PriorityBlockingQueue:

An unbounded blocking queue that uses the same ordering rules as class PriorityQueue and supplies blocking retrieval operations. While this queue is logically unbounded, attempted additions may fail due to resource exhaustion (causing OutOfMemoryError). This class does not permit null elements. A priority queue relying on natural ordering also does not permit insertion of non-comparable objects (doing so results in ClassCastException).

The internal implementation is irrelevant.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.