Quick revision
The short answer
notify() wakes one arbitrary thread from the monitor's wait set, while notifyAll() wakes all of them. notify() is safe only when every waiting thread waits for the same condition and any one of them can make progress with the change, for example identical workers waiting for a task. When producers and consumers wait on one monitor, notify() can wake a thread of the wrong kind, which rechecks, goes back to waiting, and the signal is lost; the program can end with every thread waiting while work is available, a stall that looks like deadlock. A Lock with separate Condition objects allows a targeted signal() safely.
What to remember
In this answer, we saw that notify() wakes one arbitrary waiter and notifyAll() wakes all of them. notify() is safe only when all waiters wait for the same condition and one change lets exactly one of them proceed. With producers and consumers on one monitor, notify() can wake the wrong kind of thread, which goes back to waiting while the signal is lost, and the program can stall with every thread waiting, which looks like deadlock but is not detected as one. A ReentrantLock with separate Condition objects makes targeted signal() calls safe.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
notify() wakes one arbitrary thread from the monitor's wait set, while notifyAll() wakes all of them. notify() is safe only when every waiting thread waits for the same condition and any one of them can make progress with the change, for example identical workers waiting for a task. When producers and consumers wait on one monitor, notify() can wake a thread of the wrong kind, which rechecks, goes back to waiting, and the signal is lost; the program can end with every thread waiting while work is available, a stall that looks like deadlock. A Lock with separate Condition objects allows a targeted signal() safely.
Why this question is asked
Choosing between notify and notifyAll looks like a performance decision, wake one thread or wake many, and that is how the bug sneaks in. notify is an optimisation that is correct only under specific conditions, and when they do not hold, the failure is a silent stall rather than an exception. Interviewers ask this at the ten-year level to check that we can state those conditions precisely and recognise the stall. In this answer, we will recap the wait set, state when notify is safe, walk through the lost-signal stall in a producer-consumer buffer, and see how separate conditions give us targeted wake-ups safely.
1A Quick Recap of the Wait Set
Each object's monitor has one wait set. Any thread that calls wait() on the object, for whatever reason, joins that same set. notify() picks one thread from the set, the specification does not say which, and makes it eligible to reacquire the lock. notifyAll() does this for every thread in the set.
A woken thread rechecks its condition in a while loop. If the condition is still false, it calls wait() again. That loop is what makes notifyAll safe: waking a thread that cannot proceed costs some time but is never wrong. With notify, waking the wrong thread can be wrong, because nobody else is woken.
2When notify() Is Safe
notify() is safe when two conditions hold together. First, uniform waiters: all waiting threads wait for the same condition, so whichever one is woken is interested in the change. Second, one-in, one-out: one notification lets at most one thread proceed, so waking exactly one is enough, and any of them can do the work.
A pool of identical worker threads waiting for tasks on one queue, where each put adds a single task and notifies, fits both: every waiter wants a task, and one task is enough for one worker. If either condition fails, for example if waiters wait for different things, or if one change could let several threads proceed, notify() can leave threads waiting that should be running.
| Situation | notify() | notifyAll() |
|---|---|---|
| Identical workers, one task added | safe and cheaper | safe |
| Producers and consumers on one monitor | can lose the signal and stall | safe |
| One change lets many proceed (a gate opens) | leaves others waiting | safe |
| Waiters with different conditions | unsafe | safe |
3The Lost Signal in a Producer-Consumer Buffer
Now take a bounded buffer of capacity 1, with put and take both synchronized on the buffer, and notify() instead of notifyAll(). Two consumers, C1 and C2, and two producers, P1 and P2, share the monitor.
8 messages in order. Play them, or step through them one at a time.
The one notification went to a thread that could not use it, so it was lost.
Here is one sequence that stalls. C1 and C2 call take on the empty buffer and wait. P1 puts an item, the buffer is full, and P1 notifies, waking C1. Before C1 runs, P2 tries to put, finds the buffer full and waits. C1 takes the item, the buffer is empty, and C1's notify wakes C2 rather than the waiting producer P2. C2 rechecks, the buffer is empty, so C2 waits again. Nobody notifies P2. Unless another thread happens to call put or take, P2 and C2 wait forever with space free and a producer ready to fill it.
4Why the Stall Looks Like Deadlock
In a thread dump, the stalled program shows all the involved threads in WAITING state inside Object.wait on the same monitor, and no thread holding the lock. CPU is idle and no work happens, so it looks like a deadlock. But it is not one in the classic sense: no thread holds a lock that another needs, and the JVM's deadlock detection (in jstack or ThreadMXBean.findDeadlockedThreads) reports nothing. It is a lost wake-up, and it can be just as permanent.
That makes it hard to diagnose. A useful sign is a waiting thread whose condition is clearly already true, here a producer waiting for space in an empty buffer.
5Targeted Wake-ups With Separate Conditions
The real problem is that two kinds of waiters share one wait set. A ReentrantLock can have several Condition objects, each with its own wait set. Producers wait on notFull and consumers on notEmpty, so a signal() on notEmpty can only wake a consumer, and waking exactly one is again safe, because every thread in that set waits for the same thing.
This is the design ArrayBlockingQueue uses, and it gives both correctness and efficiency: no thundering herd of wake-ups and no lost signals. The while loops stay, because spurious wakeups and late arrivals can still happen.
6Follow-up questions
Is notifyAll() always correct?
With waits inside while loops that recheck the condition, yes: every thread that can make progress is woken, and the rest go back to waiting. Its cost is extra wake-ups and lock contention when many threads wait, which matters only with large numbers of waiters.
Does the order of notify() and changing the state matter inside the synchronized block?
Not for correctness, because woken threads cannot run until the notifying thread leaves the synchronized block, so they will always see the finished change. By convention we change the state first and notify last, which reads more clearly.
Why must the while loop stay even with separate conditions?
Spurious wakeups are still allowed, and a new thread can still take the item or the space before the signalled thread reacquires the lock. Targeted signalling reduces wasted wake-ups; it does not make the condition guaranteed on return.
7Conclusion
In this answer, we saw that notify() wakes one arbitrary waiter and notifyAll() wakes all of them. notify() is safe only when all waiters wait for the same condition and one change lets exactly one of them proceed. With producers and consumers on one monitor, notify() can wake the wrong kind of thread, which goes back to waiting while the signal is lost, and the program can stall with every thread waiting, which looks like deadlock but is not detected as one. A ReentrantLock with separate Condition objects makes targeted signal() calls safe.
Open with
notify wakes one arbitrary thread from the monitor's wait set; notifyAll wakes all of them, and each rechecks its condition in its while loop. notify is safe only when two things hold: all the waiters wait for the same condition, and any one of them can make progress with the change, so waking exactly one is enough.
Then, if they want more
A pool of identical workers waiting for a task, with one task added per notify, is the typical safe case. The classic failure is a bounded buffer where producers and consumers wait on the same monitor. A consumer takes an item and calls notify, but the JVM wakes another consumer instead of the waiting producer.
That consumer sees an empty buffer and waits again, so the one signal is lost. You can end up with every thread in wait while there is space and a producer ready, a stall that looks like a deadlock in a thread dump, although no thread holds the lock and deadlock detection finds nothing.
So with mixed waiters I use notifyAll. If I want targeted wake-ups, I use a ReentrantLock with two Conditions, notFull and notEmpty, so signal only reaches a thread waiting for that exact condition, which is how ArrayBlockingQueue does it.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- java.util.concurrent, package documentation, JLS chapter 17, Threads and Locks
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "When is notify() safe to use instead of notifyAll(), and what failure does a wrong...": https://scrivio-interview-library.netlify.app/java/synchronization/5f88f1cb59/