Methods wait(), notify(), notifyAll() in Java

These methods are defined in the Object class and are intended for interaction between threads during inter-thread synchronization.

  • wait(): frees the monitor and puts the calling thread in a wait state until another thread calls the notify()/notifyAll() method;
  • notify(): continues the work of the thread on which the wait() method was previously called;
  • notifyAll(): resumes all threads on which wait() was previously called.

When the wait() method is called, the thread releases the lock on the object and transitions from the Running state to the Waiting state. The notify() method signals one of the threads waiting on the object to go into the Runnable state. However, it is impossible to determine which of the waiting threads should become operational. The notifyAll() method causes all waiting threads for the object to return to the Runnable state. If no threads are waiting on the wait() method, then nothing happens when notify() or notifyAll() is called.

A thread can call the wait() or notify() methods on a specific object only if it currently has a lock on that object. wait(), notify() and notifyAll() should only be called from synchronized code.

Difference between notify() and notifyAll()

The fact is that several threads can simultaneously "hang" on the wait() method of one monitor. When notify() is called, only one of them exits wait() and tries to capture the monitor, and then continues with the next statement after wait(). Which one will come out is unknown in advance. And when notifyAll() is called, all threads hanging on wait() exit wait(), and they all try to capture the monitor. It is clear that at any given time the monitor can be captured by only one thread, and the rest are waiting for their turn. The order of the queue is determined by the Java thread scheduler.

Why are wait() and notify() methods only called in a synchronized block?

The monitor must be captured explicitly (via a synchronized block), because the wait() and notify() methods are not synchronized.

How does the wait() method work differently with a parameter and without a parameter?

wait()

  • with no parameters frees the monitor and puts the calling thread in a wait state until another thread calls the notify()/notifyAll() method
  • with parameters will cause the thread to wait a specified amount of time or call notify()/notifyAll()

Difference between Thread.sleep() and Thread.yield() methods

The yield() method causes the thread to transition from the running state to the runnable state, allowing other threads to wake up. However, the next selected thread to run may not be different.

The sleep() method causes the current thread to sleep for a specified time, the state changes from running to waiting.

Method Thread.join()

When a thread calls join() on another thread, the current running thread will wait until the other thread it is joining has finished:

void join()        
void join(long millis) 
void join(long millis, int nanos) 


Read also:


Comments

Popular posts from this blog

Methods for reading XML in Java

XML, well-formed XML and valid XML

ArrayList and LinkedList in Java, memory usage and speed