Tuesday, January 5, 2021

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 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;

Reasons for putting these methods in Object class

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

  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.

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. Can we Start The Same Thread Twice in Java
  3. Difference Between sleep and wait in Java Multi-Threading
  4. isAlive() And join() Methods in Java Multi-Threading
  5. Print Odd-Even Numbers Using Threads And wait-notify Java Program

You may also like-

  1. Varargs (Variable-length Arguments) in Java
  2. static Import in Java With Examples
  3. Difference Between Abstract Class And Interface in Java
  4. Java Object Cloning - clone() Method
  5. How to Convert ArrayList to Array in Java
  6. Callable and Future in Java With Examples
  7. Java ArrayBlockingQueue With Examples
  8. ConcurrentHashMap in Java With Examples

3 comments:

  1. best content easily understandable

    ReplyDelete
  2. Replies
    1. In 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