What is the purpose of the wait(), notify(), and notifyAll() methods

The wait(),notify(), and notifyAll() methods are used to provide an efficient way forthreads to wait for a shared resource. When a thread executes an object's wait() method, itenters the waiting state. It only enters the ready state after another thread invokes theobject's notify() or notifyAll() methods..

Showing Answers 1 - 6 of 6 Answers

saranath

  • Mar 23rd, 2008
 

wait() will move the thread to sleep state allowing for the other thread to execute. notify() will trigger back the wait() thread to ready state and to execute further. notifyAll() will trigger all wait() threads to ready state. Later, it will be executed based on the priority and the selection based on JVM.

  Was this answer useful?  Yes

this is how signalling between threads works using 'wait and notify':

  • We can call the wait() method of any Java object, which suspends the current thread. The thread is said to be "waiting on" the given object.
  • Another thread calls the notify() method of the same Java object. This "wakes up" one of the threads waiting on that object.

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions