English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

The difference between Wait and Sleep in Java

Wait()-the thread releases ownership of this monitor and waits until another thread notifies the monitor of the object by callingnotify()method ornotifyAll()method wakes up the thread. Then, the thread waits until it can regain ownership of the monitor and resume execution.

Sleep()-This method makes the currently executing thread enter a sleep state (temporarily stop execution) for the specified milliseconds. The thread will not lose any ownership of monitors. It will send the current thread to the 'unrunnable' state within the specified time.

serial numberkeywaitingsleeping
1
Class 
The Wait() method belongs to the Object class 
The Sleep() method belongs to the Thread class 
2
Lock release 
Wait() releases the lock on the object 
It will not release the lock on the object 
3
call context
can call Wait() on the object itself 
can call Sleep() on the thread 
4.
wake-up condition
until the callnotify(),notifyAll()from the object
until at least the time expires or the call is interrupted
5
Spurious wake-up 
The program may be spurious wake-up 
It will not produce a spurious wake-up.

SynchronizedMap example

synchronized(lockedObject){
   while(condition == true){
      lockedObject.wait() //releases lockedObject lock
   }
   Thread.sleep(100); //puts current thread on Sleep
}