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?
Should be called from synchronized block :
wait()
method is always called from synchronized block i.e.wait()
method needs to lock object monitor before object on which it is called. Butsleep()
method can be called from outside synchronized block i.e.sleep()
method doesn’t need any object monitor.IllegalMonitorStateException : if
wait()
method is called without acquiring object lock thanIllegalMonitorStateException
is thrown at runtime, butsleep()
method never throws such exception.Belongs to which class :
wait()
method belongs tojava.lang.Object
class butsleep()
method belongs tojava.lang.Thread
class.Called on object or thread :
wait()
method is called on objects butsleep()
method is called on Threads not objects.Thread state : when
wait()
method is called on object, thread that holded object’s monitor goes from running to waiting state and can return to runnable state only whennotify()
ornotifyAll()
method is called on that object. And later thread scheduler schedules that thread to go from from runnable to running state. whensleep()
is called on thread it goes from running to waiting state and can return to runnable state when sleep time is up.When called from synchronized block : when
wait()
method is called thread leaves the object lock. Butsleep()
method when called from synchronized block or method thread doesn’t leaves object lock.For More Reference
There are some difference key notes i conclude after working on wait and sleep, first take a look on sample using wait() and sleep():
Example1: using wait() and sleep():
Let clarity some key notes:
Please correct me if i'm wrong.
Wait() and sleep() Differences?
Thread.sleep() Once its work completed then only its release the lock to everyone. until its never release the lock to anyone.
Object.wait() When its going to waiting stage, its will be release the key and its waiting for some of the seconds based on the parameter.
For Example:
you are take the coffee in yours right hand, you can take another anyone of the same hand, when will your put down then only take another object same type here. also. this is sleep() you sleep time you didn't any work, you are doing only sleeping.. same here also.
wait(). when you are put down and take another one mean while you are waiting , that's wait
you are play movie or anything in yours system same as player you can't play more than one at a time right, thats its here, when you close and choose another anyone movie or song mean while is called wait
Wait and sleep are two different things:
sleep()
the thread stops working for the specified duration.wait()
the thread stops working until the object being waited-on is notified, generally by other threads.A
wait
can be "woken up" by another thread callingnotify
on the monitor which is being waited on whereas asleep
cannot. Also await
(andnotify
) must happen in a blocksynchronized
on the monitor object whereassleep
does not:At this point the currently executing thread waits and releases the monitor. Another thread may do
(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 thewait
is in asynchronized
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
onObject
itself (i.e. you wait on an object's monitor) whereas you callsleep
onThread
.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 alwayswait
whilst spinning on some condition as follows:You are correct - Sleep() causes that thread to "sleep" and the CPU will go off and process other threads (otherwise known as context switching) wheras I believe Wait keeps the CPU processing the current thread.
We have both because although it may seem sensible to let other people use the CPU while you're not using it, actualy there is an overhead to context switching - depending on how long the sleep is for, it can be more expensive in CPU cycles to switch threads than it is to simply have your thread doing nothing for a few ms.
Also note that sleep forces a context switch.
Also - in general it's not possible to control context switching - during the Wait the OS may (and will for longer waits) choose to process other threads.