group-icon
Java Developers
Group for Java developers and programmers. Ask questions and get answers related to Java programming
254 Members
Join this group to post and comment.
Archana Priyadarshini
Archana Priyadarshini
Computer Science
10 Feb 2019

Difference between sleep and wait in java?

Both are used to pause thread that is currently running, however, sleep() is meant for short pause because it does not release lock, while wait() is meant for conditional wait.This is why it releases lock, which can then be developed by a different thread to alter the condition of which it is waiting.

Shruthi Pillai
Shruthi Pillai
Information Technology
1mo ago

The wait() method belongs to java.lang.Object class, thus can be called on any Object. The sleep() method belongs to java.lang.Thread class, thus can be called on Threads. 

The wait() method can only be called from Synchronized context . The sleep() method can be called from any context.

The wait() method releases the lock on an object and gives others chance to execute. The sleep() method does not releases the lock of an object for specified time or until interrupt. 

A waiting thread can be awake by notify() or notifyAll() method. A sleeping can be awaked by interrupt or time expires.