Tuesday, 7 July 2015

Why wait(), notify() and notifyAll() methods are in Object class and not in Thread class

Why wait(), notify() and notifyAll() methods are in Object class and not in Thread class is one of the 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 in 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.

General form of wait(), notify() and notifyAll() in 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;

Reasons for putting these methods in Object class

  1. First of all we have to know about what wait and notify do in order to be clear why they 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() work at the monitor level, thread which is currently holding the monitor is asked to give up that monitor through wait and through notify (or notifyAll) thread which are waiting on the object's monitor are notified that thread 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.

  2. 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.

So these are the reasons for having wait(), notify() and notifyAll() methods in Object class. Please do share with me if you know any other reason for doing the same.

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

  1. Why wait(), notify() and notifyAll() must be called inside a synchronized method or block?
  2. What if run() method called directly instead of start() method
  3. Synchronization in Java multithreading
  4. isAlive() & join() methods in Java multithreading
  5. Print odd-even numbers using threads and wait-notify
  6. Java Multi-threading interview questions

You may also like -

No comments:

Post a Comment