Interview Q&A · Java · 6 questions in this topic
wait(), notify() and Inter-Thread Communication — Java Interview Q&A
How wait, notify, and notifyAll enable inter-thread communication on a shared monitor. Part of Threads & Concurrency · 294 questions in the Java bank.
How do threads communicate?
Answer
The classic approach is shared state guarded by a lock, combined with wait, notify, and notifyAll for signalling. One thread changes the shared condition inside a synchronized region and notifies, while another thread waits for that condition. Modern Java prefers higher level channels such as BlockingQueue, Future, CompletableFuture, and concurrent collections. These hide the low level signalling and are less error prone.
Explain wait(), notify(), notifyAll().
Answer
Wait tells the current thread to release the monitor it holds and park until it is notified or interrupted. Notify wakes one arbitrary thread waiting on the same object, while notifyAll wakes every thread waiting on it, letting each re-compete for the lock. All three may only be called while holding the object monitor, otherwise they throw IllegalMonitorStateException. Wait also throws InterruptedException, and callers must recheck their condition in a loop because of spurious wakeups.
Example
synchronized(queue) { while (queue.isEmpty()) { try { queue.wait(); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } } queue.remove(); queue.notifyAll(); }
Why are they on Object instead of Thread?
Answer
Wait and notify coordinate around a shared object, not around a single thread. Every Java object has a monitor, so any object can serve as the meeting point where threads wait for a condition and where other threads signal a change. Placing these methods on Thread would wrongly suggest they control a specific thread. The design therefore puts the signalling methods on the thing threads share, which is the Object.
When is IllegalMonitorStateException thrown?
Answer
It is thrown when wait, notify, or notifyAll is called without holding the monitor of that object. In practice this means calling them outside a synchronized method or block locked on the same object. The exception signals a programming error in the locking structure. Always call these methods from inside synchronized code on the same object you wait or notify on.
Do wait/notify release the lock?
Answer
Wait releases the monitor it holds before parking, which lets another thread enter the synchronized region and change the shared condition. Notify and notifyAll do not release anything, they only move waiting threads from waiting to blocked. A woken thread cannot proceed until the notifying thread leaves its synchronized region and actually frees the lock. The source statement that notify releases the lock is therefore wrong.
💡 Interview tip: contrast wait releasing the lock with sleep keeping it, interviewers love that pair.
Which of yield/join/sleep/wait/notify/notifyAll release the lock?
Answer
Only wait releases the monitor while it parks. Yield, join, sleep, notify, and notifyAll all keep any lock the calling thread holds. This distinction matters in interviews because confusing sleep with wait is a classic error. When in doubt, remember that waiting for a condition releases the lock, while pausing or signalling does not.
Key points
- 1yield: retains locks, only a scheduling hint.
- 2join: retains locks while waiting for another thread to die.
- 3sleep: retains locks while pausing the current thread.
- 4wait: releases the monitor, then re-acquires it before returning.
- 5notify and notifyAll: retain locks, only wake waiting threads.
Learn this topic
Recommended resourcesAffiliate links · may earn us a commission