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?
wait()
is a method ofObject
class.sleep()
is a method ofThread
class.sleep()
allows the thread to go tosleep
state for x milliseconds.When a thread goes into sleep state
it doesn’t release the lock
.wait()
allows thread to release the lock andgoes to suspended state
.This thread will be active when a
notify()
ornotifAll()
method is called for the same object.From this post : http://javaconceptoftheday.com/difference-between-wait-and-sleep-methods-in-java/
wait() Method.
1) The thread which calls wait() method releases the lock it holds.
2) The thread regains the lock after other threads call either notify() or notifyAll() methods on the same lock.
3) wait() method must be called within the synchronized block.
4) wait() method is always called on objects.
5) Waiting threads can be woken up by other threads by calling notify() or notifyAll() methods.
6) To call wait() method, thread must have object lock.
sleep() Method
1) The thread which calls sleep() method doesn’t release the lock it holds.
2) sleep() method can be called within or outside the synchronized block.
3) sleep() method is always called on threads.
4) Sleeping threads can not be woken up by other threads. If done so, thread will throw InterruptedException.
5) To call sleep() method, thread need not to have object lock.
Difference between wait() and sleep()
The fundamental difference is that
wait()
is fromObject
andsleep()
is a static method ofThread
.The major difference is that
wait()
releases the lock whilesleep()
doesn’t release any lock while waiting.wait()
is used for inter-thread communication whilesleep()
is used to introduce a pause on execution, generally.wait()
should be called from inside synchronise or else we get anIllegalMonitorStateException
, whilesleep()
can be called anywhere.wait()
, you have to callnotify()
ornotifyAll()
. As forsleep(),
the thread gets started after a specified time interval.Similarities
wait
andsleep
methods are very different:sleep
has no way of "waking-up",wait
has a way of "waking-up" during the wait period, by another thread callingnotify
ornotifyAll
.Come to think about it, the names are confusing in that respect; however
sleep
is a standard name andwait
is like theWaitForSingleObject
orWaitForMultipleObjects
in the Win API.I found this post helpful. It puts the difference between
Thread.sleep()
,Thread.yield()
, andObject.wait()
in human terms. To quote:From oracle documentation page on wait() method of
Object
:notify()
method or thenotifyAll()
method for this object. In other words, this method behaves exactly as if it simply performs the callwait(0)
.This method throws
IllegalMonitorStateException
- if the current thread is not the owner of the object's monitor.InterruptedException
- if any thread interrupted the current thread before or while the current thread was waiting for a notification. The interrupted status of the current thread is cleared when this exception is thrown.From oracle documentation page on sleep() method of
Thread
class:This method throws:
IllegalArgumentException
- if the value of millis is negativeInterruptedException
- if any thread has interrupted the current thread. The interrupted status of the current thread is cleared when this exception is thrown.Other key difference:
wait()
is a non-static method (instance method) unlike static methodsleep()
(class method).