Wait and Notify : Wait Notify « Thread « SCJP






notify() sends a signal to one and only one of the threads that are waiting in that same object's waiting pool.

notify() method can NOT specify which waiting thread to notify.

If more than one Thread is waiting, the programmer cannot control which one will be returned to the runnable when notify is called. 

You can use notifyAll to nofity more than one threads.

The notifyAll call removes all waiting Threads from the wait list.
Only one of these actually gets a lock on the object and is allowed to execute the synchronized method. 

If a Thread that does not have a lock on an object attempts to call the object's wait or notify method, an IllegalMonitorStateException is thrown.

Your calls to wait or notify or notifyAll should be in synchronized code blocks.








7.8.Wait Notify
7.8.1.Wait and Notify
7.8.2.Block and wait
7.8.3.Both wait() and notify() must be called in synchronized code.
7.8.4.Using Wait() and Notify() to Control Access to a Shared Resource