Alex Rivera | Logout

Java thread wait and notify

Asked 2009-05-05T18:02:51.863
10

I have two threads. Thread A is pulling some elements from queue and thread B is adding some elements to the queue.

I want thread A to go to sleep when the queue is empty.

When thread B adds some element to the queue it should make sure that thread A is working. How can this be done in Java?

Edit
Report

1 Answer

21

Use a BlockingQueue, which is:

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.

Seriously, don't try to reinvent the wheel here.




(If you must use wait/notify, read this tutorial. But spare yourself the pain, please!)

answered 2009-05-05T18:04:13.093

Your Answer