Java Reference ยท Concurrency
Java wait() and notify()
Reference for Object.wait(), notify() and notifyAll(): coordinate threads with monitors, the guarded-wait loop, spurious wakeups and lost-notification bugs.
Every Java object has a monitor and a wait set. A thread that holds the monitor (inside a synchronized block on that object) can call wait() to release the monitor and sleep until another thread calls notify() or notifyAll() on the same object. This is the original thread-coordination mechanism of the language.
Most new code uses higher-level tools (BlockingQueue, CountDownLatch, Lock with Condition), but wait/notify still appears in old code and interviews, and it is how those tools were first built. Learn the rule that makes it correct: always wait in a loop that re-checks the condition.
synchronized (lock) { while (!ready) { lock.wait(); // releases the lock while waiting } // ready is true, and we hold the lock } // elsewhere synchronized (lock) { ready = true; lock.notifyAll(); }
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.
Syntax
public final void wait() throws InterruptedException public final void wait(long timeoutMillis) throws InterruptedException public final void notify() public final void notifyAll()
| Method | Description |
|---|---|
wait() |
Releases the monitor and blocks until notified or interrupted, then re-acquires the monitor before returning. |
wait(long ms) |
Same, but also returns after about ms milliseconds. wait(0) means wait forever. |
notify() |
Wakes one arbitrary waiting thread (it still has to re-acquire the monitor). |
notifyAll() |
Wakes every waiting thread; they compete for the monitor one by one. |
Throws: IllegalMonitorStateException if the calling thread does not own the object's monitor; InterruptedException from the wait methods.
Usage notes
- Always loop around
wait(). Threads can wake up spuriously, and another thread may have changed the condition again before you got the lock back. - Notifications are not stored. If
notify()runs before the other thread callswait(), the signal is lost. Always change shared state first, under the same lock, and test that state before waiting. - Prefer
notifyAll()unless all waiters wait for the same condition and any one of them can proceed.notify()can wake the wrong thread and leave the right one asleep. - Wait on a private lock object (
private final Object lock = new Object()) rather thanthis, so outside code cannot interfere with your monitor. - Same object on both sides.
wait()andnotify()must be called on the object whose monitor you hold; mixing two objects is a common mistake. - Do not wait on boxed values or strings that may be shared or replaced; their identity is not yours.
- Virtual threads: waiting in a monitor works, and since Java 24 it no longer pins the carrier thread.
- Alternatives:
ReentrantLockwithConditiongives several wait sets per lock;BlockingQueuehides the whole pattern.