Why wait(), notify() and notifyAll() methods are in Object class and not in Thread class is one of the Java multi-threading interview questions asked quite often.
Even if we leave the interviews aside it is important to know the concept of why anything is done that way. So let's see the reasons behind putting the wait(), notify() and notifyAll() methods in Object class though these methods are used in multi-threading scenarios.
wait(), notify() and notifyAll() methods are defined as follows in Java Object class-
public final native void wait(long timeout) throws InterruptedException; public final native void notify(); public final native void notifyAll();
There are 2 more overloaded wait methods
public final void wait() throws InterruptedException; public final void wait(long timeout, int nanos) throws InterruptedException;
- Refer Inter-thread communication using wait, notify and notifyAll to read in detail about wait(), notify() and notifyAll() methods.
Reasons for putting these methods in Object class
- First of all we have to know what wait() and notify() do in order to be clear why these methods are in Object class.
- wait- wait method tells the current thread to give up monitor and go to sleep.
- notify- Wakes up a single thread that is waiting on this object's monitor.
So you see wait() and notify() methods work at the monitor level, thread which is currently holding the monitor is asked to give up that monitor through wait() method and through notify() method (or notifyAll) threads which are waiting on the object's monitor are notified that threads can wake up.
Important point to note here is that monitor is assigned to an object not to a particular thread. That's one reason why these methods are in Object class.
To reiterate threads wait on an Object's monitor (lock) and notify() is also called on an object to wake up a thread waiting on the Object's monitor. - wait(), notify() and notifyAll() are used for inter-thread communication. But threads themselves have no knowledge of each others status. It is the shared object among the threads that acts as a communicator among the threads.
Threads lock an object, wait on an object and notify an object. When a wait method is called it checks which thread has the lock on the object and that is the thread which has to give up the lock. Same way notify() method when called looks for all the thread that are waiting to get hold of the Object's monitor and wakes one of the thread, notifyAll() wakes up all the thread that are waiting on an Object's monitor.
So it is the shared object among the thread which allows them to communicate with each other and wait(), notify() and notifyAll() are the methods used for inter-thread communication.
That's all for this topic Why wait(), notify() And notifyAll() Methods Are in Object Class And Not in Thread Class. If you have any doubt or any suggestions to make please drop a comment. Thanks!
Related Topics
You may also like-
best content easily understandable
ReplyDeletewhat do you mean by monitor?
ReplyDeleteIn simple terms you can say monitor is the lock associated with every object. In Java every object has a monitor logically associated with it which gives the object mutual exclusion capability.
Delete