Java – wait(), notify(), notifyAll() in Thread

wait(), notify() and notifyAll():

  • wait() method forces the current thread to wait until some other thread invokes notify() or notifyAll() on the same object.
  • notify() method for waking up threads that are waiting for an access to this object’s monitor.
  • notifyAll() method simply wakes all threads that are waiting on this object’s monitor.

Syntax of Calling:

wait()notify()notifyAll()
synchronized( lockObject )
{
            while(!condition )
            {
                        lockObject.wait();
            }
            //take the action here;
}
synchronized(lockObject)
{
            //establish_the_condition;
            lockObject.notify();
            //any additional code
}
synchronized(lockObject)
{
            establish_the_condition;
            lockObject.notifyAll();
}
Scroll to Top