Interview Q&A / Java

IntermediateQuick answer1 min readUpdated 3 Oct 2026

What is the difference between wait() and sleep() in Java?

SHORT ANSWER

sleep() pauses the current thread for a time and keeps any lock it holds. wait() must be called inside a synchronized block, releases that lock, and pauses until notify or notifyAll is called or the timeout ends.

Explain it simply

sleep() is a person dozing off in the doorway of a room: nobody else can get through, because they still hold the door. wait() is stepping out of the room and leaving the door free, with a note saying "call me when there is something to do".

Example

java
class Box {
    private boolean ready = false;

    synchronized void waitForItem() throws InterruptedException {
        while (!ready) {
            wait();                 // releases the lock on this Box while waiting
        }
        System.out.println("item taken");
    }

    synchronized void putItem() {
        ready = true;
        notifyAll();                // wakes the waiting threads
    }
}

What to say in the interview

"sleep is a static method of Thread, needs no lock, and keeps held locks. wait is an instance method of Object, requires owning the object's monitor or it throws IllegalMonitorStateException, and gives up that monitor while waiting. I always call wait inside a while loop that rechecks the condition, because of spurious wakeups and because another thread may have changed the state."

Common mistakes

  • Using if instead of while around wait().
  • Calling wait() or notify() outside synchronized.

#java#multithreading#wait-notify

Related questions

Practise more Java questions

Test your recall with short answers, then return to the detailed explanations when you need context.

Open Java quick answers →

Get new questions like this by email

One short email when something new and useful lands. No spam, unsubscribe in one tap.

By subscribing you agree to the Privacy Policy.