Object Class Methods

Why wait() and notify() are used only for threads, why not to objects?

Questions by anilkumar05

Showing Answers 1 - 18 of 18 Answers

The wait() and notify() methods are object-specific. The wait() method suspends the current thread of execution, and tells the object to keep track of the suspended thread. The notify() method tells the object to wake up the suspended threads that it is currently keeping track of. Since wait() and notify() are object specific, they must be used within code that is synchronized on the object in question.

It is also important to use state variables when using wait() and notify(), as threads can be woken up by conditions other than notify().

SomayNakhal

  • May 23rd, 2010
 

wait() and notify() are NOT needed for objects statements/method calls within the same thread, because execution on objects running in the same thread will run one at a time and not overlap.

If execution happens in different threads then there is a chance for them to overlap and the need for mechanism to manage concurrent execution arise.

jeetu1607

  • May 30th, 2010
 

wait(), notify() and notifyAll() are in Object class because the lock for these are set on a object and these can be called on any object. Any threads accessing the object will be put into wait status whenever a wait() on that object is called. Any thread will resume execution iff the notify() or notifyAll() method is called for that object.

  Was this answer useful?  Yes

The wait() and notify() methods are object-specific. The wait() method suspends the current thread of execution, and tells the object to keep track of the suspended thread. The notify() method tells the object to wake up the suspended threads that it is currently keeping track of. Since wait() and notify() are object specific, they must be used within code that is synchronized on the object in question.

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