Concurrent put calls on a BlockingQueue in Java
I know that concurrent adds to 开发者_开发问答an stl queue in c++ can cause issues, and the way to solve this is adding a mutex lock around all add/remove calls.
But I am programming in Java at the moment, and I'm using BlockingQueue. The documentation only says that the thread that calls put/take on a BlockingQueue object gets blocked implicitly until there's room to put/there's something to take respectively. However, it does not mention anything about concurrent put/take calls. Would I need to protect these with a mutex lock?
No, blocking-queues are thread-safe. From the docs:
BlockingQueue implementations are thread-safe. All queuing methods achieve their effects atomically using internal locks or other forms of concurrency control
From the documentation:
BlockingQueue implementations are thread-safe.
Therefore, you don't need a lock.
精彩评论