What is the difference between a wait()
and sleep()
in Threads?
Is my understanding that a wait()
-ing Thread is still in runni
One potential big difference between sleep/interrupt and wait/notify is that
Generating an exception when not needed is inefficient. If you have threads communicating with each other at a high rate, then it would be generating a lot of exceptions if you were calling interrupt all the time, which is a total waste of CPU.
wait
releases the lock and sleep
doesn't. A thread in waiting state is eligible for waking up as soon as notify
or notifyAll
is called. But in case of sleep
the thread keeps the lock and it'll only be eligible once the sleep time is over.
The fundamental difference is that wait()
is from Object
and sleep()
is a static method of Thread
.
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.
wait()
should be called from inside synchronise or else we get an IllegalMonitorStateException
, while sleep()
can be called anywhere.
To start a thread again from wait()
, you have to call notify()
or notifyAll()
. As for sleep(),
the thread gets started after a specified time interval.
From oracle documentation page on wait() method of Object
:
public final void wait()
notify()
method or the notifyAll()
method for this object. In other words, this method behaves exactly as if it simply performs the call wait(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:
public static void sleep(long millis)
This method throws:
IllegalArgumentException
- if the value of millis is negative
InterruptedException
- 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 method sleep()
(class method).
This is a very simple question, because both these methods have a totally different use.
The major difference is to wait to release the lock or monitor while sleep doesn't release any lock or monitor while waiting. Wait is used for inter-thread communication while sleep is used to introduce pause on execution.
This was just a clear and basic explanation, if you want more than that then continue reading.
In case of wait()
method thread goes in waiting state and it won't come back automatically until we call the notify()
method (or notifyAll()
if you have more then one thread in waiting state and you want to wake all of those thread). And you need synchronized or object lock or class lock to access the wait()
or notify()
or notifyAll()
methods. And one more thing, the wait()
method is used for inter-thread communication because if a thread goes in waiting state you'll need another thread to wake that thread.
But in case of sleep()
this is a method which is used to hold the process for few seconds or the time you wanted. Because you don't need to provoke any notify()
or notifyAll()
method to get that thread back. Or you don't need any other thread to call back that thread. Like if you want something should happen after few seconds like in a game after user's turn you want the user to wait until the computer plays then you can mention the sleep()
method.
And one more important difference which is asked often in interviews: sleep()
belongs to Thread
class and wait()
belongs to Object
class.
These are all the differences between sleep()
and wait()
.
And there is a similarity between both methods: they both are checked statement so you need try catch or throws to access these methods.
I hope this will help you.
In my opinion, the main difference between both mechanisms is that sleep/interrupt is the most basic way of handling threads, whereas wait/notify is an abstraction aimed to do thread inter-communication easier. This means that sleep/interrupt can do anything, but that this specific task is harder to do.
Why is wait/notify more suitable? Here are some personal considerations:
It enforces centralization. It allows to coordinate the communication between a group of threads with a single shared object. This simplifies the work a lot.
It enforces synchronization. Because it makes the programmer wrap the call to wait/notify in a synchronized block.
It's independent of the thread origin and number. With this approach you can add more threads arbitrarily without editing the other threads or keeping a track of the existing ones. If you used sleep/interrupt, first you would need to keep the references to the sleeping threads, and then interrupt them one by one, by hand.
An example from the real life that is good to explain this is a classic restaurant and the method that the personnel use to communicate among them: The waiters leave the customer requests in a central place (a cork board, a table, etc.), ring a bell, and the workers from the kitchen come to take such requests. Once that there is any course ready, the kitchen personnel ring the bell again so that the waiters are aware and take them to the customers.