Wait(), notify() and notyfyAll() methods

Why wait(), notify() and notyfyAll() methods are avialble in Object class but not in Thread class? Explain.

Questions by varalakshmi.k

Editorial / Best Answer

janswain  

  • Member Since Sep-2008 | Sep 25th, 2008


Each object in java is associated with a lock and the wait and notify methods are not associated with a thread but with a lock, so coupling locks and objects meant that each object should have wait and notify methods that operate on that object’s lock.

Java provides a way to lock the code for a thread which is currently executing it using the synchorinized keyword, and making other threads that wish to use it wait until the first thread is finished.These other threads are placed in the waiting state .Once the first thread completes it task than it notifies others in the queue by giving the lock to JVM.Now JVM decides which thread will exceute next by its internal algo.Hence, the wait and notify methods has to be in Object class as it acquires lock and has to notify others.....

Showing Answers 1 - 21 of 21 Answers

lks007

  • Aug 22nd, 2008
 

As wait(), notify() and notyfyAll() methods are present in Object class which is a super class for all the classes. There is no need to redefine the same methods in other classes as they all will inherit them from there super class Object.

wait(), notify() and notyfyAll() methods these are inherited from Object class.
if you call wait() method, it will enter in to the monitor for specfic time or ideal time when call notify() it will enter from the waiting pool to ready state
When notifyAll() calls then it will bring all of the waiting threads from waiting pool to ready state

janswain

  • Sep 25th, 2008
 

Each object in java is associated with a lock and the wait and notify methods are not associated with a thread but with a lock, so coupling locks and objects meant that each object should have wait and notify methods that operate on that object’s lock.

Java provides a way to lock the code for a thread which is currently executing it using the synchorinized keyword, and making other threads that wish to use it wait until the first thread is finished.These other threads are placed in the waiting state .Once the first thread completes it task than it notifies others in the queue by giving the lock to JVM.Now JVM decides which thread will exceute next by its internal algo.Hence, the wait and notify methods has to be in Object class as it acquires lock and has to notify others.....

pandeysc99

  • Sep 12th, 2009
 

All the common and system security method is defined in Object class and "wait, notify, notrifyAll" is related with system and process security so they are defined in object class in spite of Thread class;

  Was this answer useful?  Yes

A lock is done on an object. It is an obect which is locked, hence these methods are characteristics of the object, hence defined in the Object class.

  Was this answer useful?  Yes

A thread having a lock on the shared object (by using synchronized constructs) can invoke these methods on that shared Object. It gives a hint to JVM scheduler to pick or block the next thread contending for that lock.

Had these methods were part of Thread class, JVM thread scheduler would have no work. Participating threads would have to coordinate among themself for the lock control.

Ofcourse these methods could have declared in Thread class too by Java Specs experts, but in that case, JVM implementation startegy would have different.

  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