开发者

Analysing a BlockingQueue usage example

开发者 https://www.devze.com 2023-03-16 17:38 出处:网络
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)

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 h开发者_开发问答appening 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.


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.


BlockingQueue is just an interface. This implementation could be using synchronzed blocks, Lock or be lock-free. AFAIK most methods use Lock in the implementation.

0

精彩评论

暂无评论...
验证码 换一张
取 消