Java / Java Concurrency and Multi Threading in Java 17 and Java 21 Interview questions
What is the difference between wait() and sleep()?
| wait() | sleep() |
Defined on Object. | Defined as a static method on Thread. |
| Must be called while holding the object's monitor (inside synchronized). | Can be called from anywhere, no lock required. |
| Releases the held lock while waiting. | Does not release any lock it holds. |
Woken by notify()/notifyAll(), or a timeout if given. | Wakes automatically after the specified duration. |
wait() is a coordination primitive: a thread gives up the CPU and the lock, telling other threads "wake me up when a condition changes." sleep() is a simple pause and has nothing to do with inter-thread signaling or locks.
Both throw InterruptedException if the thread is interrupted while waiting, which needs to be handled or have the interrupt status restored.
More Related questions...