These methods wait(), notify() and notifyAll() are implemented as final method in the Object class thus available to all the classes, as Object class is the super class of all the classes in Java.
wait(), notify() and notifyAll() methods are in Object class not in Thread class
The following are the reasons behind having 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.
- 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.
The following are the reasons why wait(), notify() and notifyAll() must be called inside a synchronized method or block:
- Every object created in Java has one associated monitor (mutually exclusive lock). Only one thread can own a monitor at any given time.
- For achieving synchronization in Java this monitor is used. When any thread enters a synchronized method/block it acquires the lock on the specified object. When any thread acquires a lock it is said to have entered the monitor. All other threads which need to execute the same shared piece of code (locked monitor) will be suspended until the thread which initially acquired the lock releases it.
- wait method tells the current thread (thread which is executing code inside a synchronized method or block) to give up monitor and go to waiting state.
- notify method wakes up a single thread that is waiting on this object's monitor.
- notifyAll method wakes up all the threads that called wait() on the same object.
wait() method in JavaWait method tells the current thread (thread which is executing code inside a synchronized method or block) to give up monitor and go to sleep, until another thread invokes the notify() or notifyAll() method for this object.
General form of wait method in Java
- public final void wait() throws InterruptedException
- public final void wait(long timeout) throws InterruptedException : Causes the current thread to wait until either another thread invokes the notify() method or the notifyAll() method for this object, or a specified amount of time has elapsed.
- public final void wait(long timeout, int nanos) throws InterruptedException: Causes the current thread to wait until another thread invokes the notify() method or the notifyAll() method for this object, or some other thread interrupts the current thread, or a certain amount of real time has elapsed.
notify() method in JavaWakes up a single thread that is waiting on this object's monitor. If more than one threads are waiting on this object, one of them is chosen to be awakened. The choice is arbitrary and occurs at the discretion of the implementation.
Note that the thread which comes out of waiting because of the notify() method will not be able to proceed until the current thread relinquishes the lock on this object. The awakened thread just changes to the runnable state and it is ready to be scheduled again. The awakened thread will compete in the usual manner with any other threads that might be actively competing to synchronize on this object. The awakened thread enjoys no reliable privilege or disadvantage in being the next thread to lock this object.
When awakened thread(thread which has come out of waiting because of notify() method) gains control of the object, all its synchronization claims on the object are restored to the situation as of the time that the wait method was invoked that is on return from the wait method, the synchronization state of the object and of thread is exactly as it was when the wait method was invoked.
notifyAll() method in JavaWakes up all the threads that called wait() on the same object. As explained in notify() any one of the threads will be granted access to the object.
Once wait is called on an object the thread that is currently executing with in the synchronized context waits until notify() or notifyAll() method is called. But there is a possibility that a waiting thread resumes again even when notify() or notifyAll() are not called (this will rarely occur in practice). This is known as spurious wakeup in Java.
To guard against spurious wakeup the recommendation is that call to wait() method should be with in a loop that checks the condition on which the thread is waiting.
while (condition does not hold)
... // Perform action appropriate to condition
Producer-Consumer example using wait, notify
class Producer implements Runnable
this.sharedListObj = sharedListObj;
public void run()
int i = 0;
// While condition as mandated to avoid spurious wakeup
while(sharedListObj.size() >= 1)
catch (InterruptedException e)
// Putting value in the list
System.out.println("Adding to queue - "
+ Thread.currentThread().getName() + " " + ++i);
// To get out of while(true) loop, putting
// only 5 values
if(i > 4) break;