What is the difference between a wait()
and sleep()
in Threads?
Is my understanding that a wait()
-ing Thread is still in runni
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.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.
Sleep() take the key, its never release the key to anyone, when its work completed then only its release then only take the key waiting stage threads.
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
sleep()
method causes the current thread to move from running state to block state for a specified time. If the current thread has the lock of any object then it keeps holding it, which means that other threads cannot execute any synchronized method in that class object.
wait()
method causes the current thread to go into block state either for a specified time or until notify, but in this case the thread releases the lock of the object (which means that other threads can execute any synchronized methods of the calling object.
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
}
wait()
is a method of Object
class.
sleep()
is a method of Thread
class.
sleep()
allows the thread to go to sleep
state for x milliseconds.
When a thread goes into sleep state it doesn’t release the lock
.
wait()
allows thread to release the lock and goes to suspended state
.
This thread will be active when a notify()
or notifAll()
method is
called for the same object.
Here, I have listed few important differences between wait()
and sleep()
methods.
PS: Also click on the links to see library code (internal working, just play around a bit for better understanding).
wait()
method releases the lock. wait()
is the method of Object
class.wait()
is the non-static method - public final void wait() throws InterruptedException { //...}
wait()
should be notified by notify()
or notifyAll()
methods.wait()
method needs to be called from a loop in order to deal with false alarm.
wait()
method must be called from synchronized context (i.e. synchronized method or block), otherwise it will throw IllegalMonitorStateException
sleep()
method doesn't release the lock.sleep()
is the method of java.lang.Thread
class.sleep()
is the static method - public static void sleep(long millis, int nanos) throws InterruptedException { //... }
sleep()
is completed.sleep()
better not to call from loop(i.e. see code below).sleep()
may be called from anywhere. there is no specific requirement. Ref: Difference between Wait and Sleep
Code snippet for calling wait and sleep method
synchronized(monitor){
while(condition == true){
monitor.wait() //releases monitor lock
}
Thread.sleep(100); //puts current thread on Sleep
}