Does add() on LinkedBlockingQueue notify waiting threads?
I have a consumer thread taking elements from a LinkedBlockingQueue
, and I make it sleep manually when it's e开发者_如何学Cmpty. I use peek()
to see if the queue empty because I have to do stuff before sending the thread to sleep, and I do that with queue.wait()
.
So, when I'm in another thread and add()
an element to the queue, does that automatically notify the thread that was wait()
ing on the queue?
Yes it does. Or rather, it does using a more-efficient internal lock object and not the outer queue object's lock; if you want to sleep until something arrives in the queue, do a blocking take()
. (If you have other things to do while waiting, consider whether a blocking queue is the correct way of receiving messages from elsewhere.)
精彩评论