Medium👤 3-5 years👤 8-15 years 1 min read

Why must wait() always be called in a while-loop, not an if?

Asked inAmazonMicrosoftDeloitte
#wait#notify#guarded block#spurious wakeup#condition
Report issue

⚡ 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.

Coffee Chat Question

Concept Made Simple

Why must wait() always be called in a while-loop, not an if?

🧠Mind Map Answer

Remember It Faster

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.

⌨️Hands-on Keyboard

Learn by Doing

java
synchronized (lock) {
    while (!ready) {     // NOT if
        lock.wait();
    }
    consume();
}

🔥What If?

Think Beyond the Expected

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 wakeupscondition re-checkwhile not ifnotify vs notifyAllmissed 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

  • 1What is a missed-signal (lost wakeup) bug?
  • 2How do Lock + Condition improve on wait/notify?
  • 3Why prefer BlockingQueue over hand-rolled wait/notify?

🧩Related Technologies

Lock/ConditionBlockingQueueCountDownLatch

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.
Open inChatGPTGeminiClaude

Was this answer helpful?

Support our platform by exploring our recommended products.

As an Amazon affiliate, purchases through these links may earn us a small commission — at no extra cost to you. It helps keep Full Stack Interview Guru free.

Related Questions