Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between wait() and sleep()

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?

like image 772
Geek Avatar asked Jun 24 '09 06:06

Geek


People also ask

What is the difference between wait and sleep in Python?

Difference between wait() and sleep() The major difference is that wait() releases the lock while sleep() doesn't release any lock while waiting. wait() is used for inter-thread communication while sleep() is used to introduce a pause on execution, generally.

What are the wait () and sleep () methods?

At the time of the Synchronization, the Wait() method releases obj. At the time of the Synchronization, the Sleep() method doesn't release the obj, i.e., lock. 5. We can call the Wait () method only from the Synchronized context.

What's the difference between the methods sleep () and wait () Mcq?

Whenever a thread calls wait() method, it releases the lock or monitor it holds and when it calls sleep() method, it doesn't release the lock or monitor it holds. This is the main difference between wait() and sleep() methods.

What is difference between sleep () and interrupt () method in Java?

You use interrupt() when you want to interrupt() another Thread which may be waiting on something. You use sleep() when you want this thread to pause for a bit.


2 Answers

A wait can be "woken up" by another thread calling notify on the monitor which is being waited on whereas a sleep cannot. Also a wait (and notify) must happen in a block synchronized on the monitor object whereas sleep does not:

Object mon = ...; synchronized (mon) {     mon.wait(); }  

At this point the currently executing thread waits and releases the monitor. Another thread may do

synchronized (mon) { mon.notify(); } 

(on the same mon object) and the first thread (assuming it is the only thread waiting on the monitor) will wake up.

You can also call notifyAll if more than one thread is waiting on the monitor – this will wake all of them up. However, only one of the threads will be able to grab the monitor (remember that the wait is in a synchronized block) and carry on – the others will then be blocked until they can acquire the monitor's lock.

Another point is that you call wait on Object itself (i.e. you wait on an object's monitor) whereas you call sleep on Thread.

Yet another point is that you can get spurious wakeups from wait (i.e. the thread which is waiting resumes for no apparent reason). You should always wait whilst spinning on some condition as follows:

synchronized {     while (!condition) { mon.wait(); } } 
like image 198
oxbow_lakes Avatar answered Sep 21 '22 16:09

oxbow_lakes


One key difference not yet mentioned is that while sleeping a Thread does not release the locks it holds, while waiting releases the lock on the object that wait() is called on.

synchronized(LOCK) {     Thread.sleep(1000); // LOCK is held }   synchronized(LOCK) {     LOCK.wait(); // LOCK is not held } 
like image 20
Robert Munteanu Avatar answered Sep 20 '22 16:09

Robert Munteanu