Difference Between Thread.sleep() and Object.wait()
Thread.sleep() and Object.wait() both pause a thread, which is why they are often confused. sleep() pauses the current thread for a fixed time and keeps any locks it holds. wait() pauses the thread until another thread signals it with notify() or notifyAll(), and releases the lock while it waits. In this lesson, you will see the most important difference, what happens to the lock, in code, and then compare the two methods side by side.
Example: Who Can Get the Lock?
Let's see an example where one thread pauses while holding a lock, first with sleep and then with wait, and a second thread tries to take the same lock.
public class SleepVsWait {
static final Object lock = new Object();
static void test(String label, boolean useWait)
throws InterruptedException {
Thread pauser = new Thread(() -> {
synchronized (lock) {
try {
if (useWait) {
lock.wait(500);
} else {
Thread.sleep(500);
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
});
pauser.start();
Thread.sleep(100);
long start = System.nanoTime();
synchronized (lock) {
long waited = (System.nanoTime() - start) / 1_000_000;
System.out.println(label + ": Srihitha got the lock after waiting "
+ (waited > 200 ? "a long time" : "almost no time"));
}
pauser.join();
}
public static void main(String[] args) throws InterruptedException {
test("sleep()", false);
test("wait() ", true);
}
}sleep(): Srihitha got the lock after waiting a long time
wait() : Srihitha got the lock after waiting almost no timeExplanation: With sleep, the pausing thread kept the lock, so the main thread had to wait about 400 milliseconds until it woke up and left the block. With wait, the pausing thread released the lock as soon as it started waiting, so the main thread got it almost immediately.
Differences Between sleep() and wait()
Thread.sleep() | Object.wait() | |
|---|---|---|
| Defined in | Thread (static) | Object (instance, final) |
| Purpose | Pause for a time | Wait for a condition signaled by another thread |
| Lock | Kept | Released while waiting, re-taken before returning |
| Must hold the lock | No | Yes, inside synchronized, or IllegalMonitorStateException |
| Wakes up when | The time passes (or interrupted) | notify/notifyAll, timeout, interrupt or spuriously |
| Thread state | TIMED_WAITING | WAITING (or TIMED_WAITING with a timeout) |
| Throws | InterruptedException | InterruptedException |
| Called in a loop | Not needed | Yes, recheck the condition |
Common Mistakes
Calling sleep inside synchronized and expecting other threads to proceed. They're blocked until it finishes.
Calling wait outside synchronized. It throws.
Polling with sleep instead of waiting for a signal. It wastes time and reacts slowly.
Interview Corner
What is the difference between sleep() and wait()?
Thread.sleep(ms) is a static method that pauses the current thread for a period without releasing any monitors it holds, and can be called anywhere. Object.wait() is an instance method that must be called while holding that object's monitor; it releases the monitor and suspends the thread until another thread calls notify or notifyAll on the same object, the timeout expires, or it's interrupted, then re-acquires the monitor before returning. sleep is for timing; wait is for inter-thread coordination and should be used in a loop.
Practice
- Easy: Print the thread state of a thread that is in
sleep(1000)and of one inlock.wait(). - Harder: Replace a "sleep 100 ms and check a flag" loop with
wait/notifyAll, and measure how much faster the waiting thread reacts.
Key Takeaways
Was this tutorial helpful?