Why must wait() always be called in a while-loop, not an if?
Reviewed by Gurusankar M.
Short Answer
Because of spurious wakeups and the gap between notify and re-acquiring the lock: a thread can wake when the condition is still false. A while-loop re-checks the condition after waking; an if would proceed on a false assumption.
On this page
Concept Made Simple
Coffee Chat Question
“Why must wait() always be called in a while-loop, not an if?”
Remember It Faster
Mind Map Answer
Pattern: synchronized(lock){ while(!ready) lock.wait(); /* act */ }. The while re-tests ready every wake-up — guarding against spurious wakeups and another thread changing state between notify and reacquire.
Learn by Doing
Hands-on Keyboard
synchronized (lock) {
while (!ready) { // NOT if
lock.wait();
}
consume();
}Think Beyond the Expected
What If?
notify() vs notifyAll() — when is notify() unsafe?
notify() wakes one arbitrary waiter. If waiters are waiting on different conditions (e.g. mixed producers/consumers on one lock), it may wake the 'wrong' one, which re-waits — and the right thread never runs (a missed-signal lockup). notifyAll() is safer unless you've proven all waiters are equivalent.
Real World
Hand-rolled wait/notify is error-prone; most code should use BlockingQueue or Lock/Condition. But interviewers probe the while-loop rule to test memory-model understanding.
Interviewer's Expectation
Keywords they're listening for:
- spurious wakeups
- condition re-check
- while not if
- notify vs notifyAll
- missed signal
Common Mistakes
- Using if instead of while
- notify() when waiters have different conditions
- Calling wait/notify without holding the monitor
Best Practices
- Always guard with a while-loop condition
- Prefer notifyAll() unless proven safe
- Use higher-level concurrency utilities
Follow-up Questions
- What is a missed-signal (lost wakeup) bug?
- How do Lock + Condition improve on wait/notify?
- Why prefer BlockingQueue over hand-rolled wait/notify?
Continue Learning with AI
Take this question deeper with your favourite AI assistant. Pick a depth, copy the prompt, or open it directly — AI is your learning companion, not a shortcut.
Plain-language foundations
I'm preparing for a software engineering interview and want to understand this from scratch, as a beginner. Topic: Locks (Multithreading) Interview question: "Why must wait() always be called in a while-loop, not an if?" Please: 1. Explain the core idea in simple, plain language, using an everyday analogy. 2. Define any technical terms you use. 3. Walk through one small, concrete example. 4. Finish with a single sentence I can easily remember. Keep the tone friendly and assume I'm new to this topic.
Was this answer helpful?
Affiliate links
Tools for Your Developer Workspace
Equipment for a comfortable, productive setup.
As an Amazon Associate I earn from qualifying purchases.
- MonitorView on Amazon : BenQ EW2790U 27-inch Monitor (opens in a new tab)
BenQ EW2790U 27-inch Monitor
- LaptopView on Amazon : Acer Aspire One Laptop (opens in a new tab)
Acer Aspire One Laptop
- Office ChairView on Amazon : Wakefit Gravita Ergonomic Office Chair (opens in a new tab)
Wakefit Gravita Ergonomic Office Chair