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?
Here wait() will be in the waiting state till it notify by another Thread but where as sleep() will be having some time..after that it will automatically transfer to the Ready state...
source : http://www.jguru.com/faq/view.jsp?EID=47127
sleep
is a method ofThread
,wait
is a method ofObject
, sowait/notify
is a technique of synchronizing shared data in Java (using monitor), butsleep
is a simple method of thread to pause itself.Example about sleep doesn’t release lock and wait does
Here there are two classes :
Singleton : This is singleton class with two static methods getInstance() and getInstance(boolean isWait).
and
Now run this example you will get below output :
Here Singleton instances created by threadA and threadB are same. It means threadB is waiting outside until threadA release it’s lock.
Now change the Singleton.java by commenting Thread.sleep(500); method and uncommenting Singleton.class.wait(500); . Here because of Singleton.class.wait(500); method threadA will release all acquire locks and moves into the “Non Runnable” state, threadB will get change to enter in synchronized block.
Now run again :
Here Singleton instances created by threadA and threadB are NOT same because of threadB got change to enter in synchronised block and after 500 milliseconds threadA started from it’s last position and created one more Singleton object.
There are a lot of answers here but I couldn't find the semantic distinction mentioned on any.
It's not about the thread itself; both methods are required as they support very different use-cases.
sleep()
sends the Thread to sleep as it was before, it just packs the context and stops executing for a predefined time. So in order to wake it up before the due time, you need to know the Thread reference. This is not a common situation in a multi-threaded environment. It's mostly used for time-synchronization (e.g. wake in exactly 3.5 seconds) and/or hard-coded fairness (just sleep for a while and let others threads work).wait()
, on the contrary, is a thread (or message) synchronization mechanism that allows you to notify a Thread of which you have no stored reference (nor care). You can think of it as a publish-subscribe pattern (wait
== subscribe andnotify()
== publish). Basically using notify() you are sending a message (that might even not be received at all and normally you don't care).To sum up, you normally use
sleep()
for time-syncronization andwait()
for multi-thread-synchronization.They could be implemented in the same manner in the underlying OS, or not at all (as previous versions of Java had no real multithreading; probably some small VMs doesn't do that either). Don't forget Java runs on a VM, so your code will be transformed in something different according to the VM/OS/HW it runs on.
wait
releases the lock andsleep
doesn't. A thread in waiting state is eligible for waking up as soon asnotify
ornotifyAll
is called. But in case ofsleep
the thread keeps the lock and it'll only be eligible once the sleep time is over.