agentofuser
agentofuser

Reputation: 9327

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 empty. 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?

Upvotes: 1

Views: 2041

Answers (1)

Donal Fellows
Donal Fellows

Reputation: 137717

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.)

Upvotes: 2

Related Questions