What is the difference between a wait()
and sleep()
in Threads?
Is my understanding that a wait()
-ing Thread is still in running mode and uses CPU cycles but a sleep()
-ing does not consume any CPU cycles correct?
Why do we have both wait()
and sleep()
: how does their implementation vary at a lower level?
sleep() is a method which is used to hold the process for few seconds or the time you wanted but in case of wait() method thread goes in waiting state and it won’t come back automatically until we call the notify() or notifyAll().
The major difference is that wait() releases the lock or monitor while sleep() doesn’t releases any lock or monitor while waiting. Wait is used for inter-thread communication while sleep is used to introduce pause on execution, generally.
Thread.sleep() sends the current thread into the “Not Runnable” state for some amount of time. The thread keeps the monitors it has acquired — i.e. if the thread is currently in a synchronized block or method no other thread can enter this block or method. If another thread calls t.interrupt() it will wake up the sleeping thread. Note that sleep is a static method, which means that it always affects the current thread (the one that is executing the sleep method). A common mistake is to call t.sleep() where t is a different thread; even then, it is the current thread that will sleep, not the t thread.
object.wait() sends the current thread into the “Not Runnable” state, like sleep(), but with a twist. Wait is called on an object, not a thread; we call this object the “lock object.” Before lock.wait() is called, the current thread must synchronize on the lock object; wait() then releases this lock, and adds the thread to the “wait list” associated with the lock. Later, another thread can synchronize on the same lock object and call lock.notify(). This wakes up the original, waiting thread. Basically, wait()/notify() is like sleep()/interrupt(), only the active thread does not need a direct pointer to the sleeping thread, but only to the shared lock object.
Let categorize all above points :
Call on:
Synchronized:
Hold lock:
Wake-up condition:
Usage:
Ref:diff
sleep
andwait
wait(1000)
causes the current thread to sleep up to one second.notify()
ornotifyAll()
method call.sleep(1000)
causes the current thread to sleep for exactly 1 second.In simple words, wait is wait Until some other thread invokes you whereas sleep is "dont execute next statement" for some specified period of time.
Moreover sleep is static method in Thread class and it operates on thread, whereas wait() is in Object class and called on an object.
Another point, when you call wait on some object, the thread involved synchronize the object and then waits. :)
This is a very simple question, because both these methods have a totally different use.
The major difference is to wait to release the lock or monitor while sleep doesn't release any lock or monitor while waiting. Wait is used for inter-thread communication while sleep is used to introduce pause on execution.
This was just a clear and basic explanation, if you want more than that then continue reading.
In case of
wait()
method thread goes in waiting state and it won't come back automatically until we call thenotify()
method (ornotifyAll()
if you have more then one thread in waiting state and you want to wake all of those thread). And you need synchronized or object lock or class lock to access thewait()
ornotify()
ornotifyAll()
methods. And one more thing, thewait()
method is used for inter-thread communication because if a thread goes in waiting state you'll need another thread to wake that thread.But in case of
sleep()
this is a method which is used to hold the process for few seconds or the time you wanted. Because you don't need to provoke anynotify()
ornotifyAll()
method to get that thread back. Or you don't need any other thread to call back that thread. Like if you want something should happen after few seconds like in a game after user's turn you want the user to wait until the computer plays then you can mention thesleep()
method.And one more important difference which is asked often in interviews:
sleep()
belongs toThread
class andwait()
belongs toObject
class.These are all the differences between
sleep()
andwait()
.And there is a similarity between both methods: they both are checked statement so you need try catch or throws to access these methods.
I hope this will help you.
The methods are used for different things.
Thread.sleep(n) can be interrupted, but Object.wait() must be notified. It's possible to specify the maximum time to wait:
Object.wait(5000)
so it would be possible to usewait
to, er,sleep
but then you have to bother with locks.Neither of the methods uses the cpu while sleeping/waiting.
The methods are implemented using native code, using similar constructs but not in the same way.
Look for yourself: Is the source code of native methods available? The file
/src/share/vm/prims/jvm.cpp
is the starting point...One potential big difference between sleep/interrupt and wait/notify is that
interrupt()
duringsleep()
always throws an exception (e.g. InterruptedException), whereasnotify()
duringwait()
does not.Generating an exception when not needed is inefficient. If you have threads communicating with each other at a high rate, then it would be generating a lot of exceptions if you were calling interrupt all the time, which is a total waste of CPU.