Search code examples
javamultithreadingblockingqueue

Analysing a BlockingQueue usage example


I was looking at the "usage example based on a typical producer-consumer scenario" at: http://download.oracle.com/javase/1.5.0/docs/api/java/util/concurrent/BlockingQueue.html#put(E)

Is the example correct?

I think the put and take operations need a lock on some resource before proceeding to modify the queue, but that is not happening here.

Also, had this been a Concurrent kind of a queue, the lack of locks would have been understandable since atomic operations on a concurrent queue do not need locks.


Solution

  • I do not think there is something to add to what is written in api:

    A Queue that additionally supports operations that wait for the queue to become non-empty when retrieving an element, and wait for space to become available in the queue when storing an element.

    BlockingQueue implementations are thread-safe. All queuing methods achieve their effects atomically using internal locks or other forms of concurrency control.