Difference between sleep() and wait() in Java
sleep() is a method which is used to pause the process for few seconds or the time we want to. But in case of wait() method, thread goes in waiting state and it won’t come back automatically until we call the notify()
or notifyAll()
.
The major difference is that wait()
releases the lock or monitor while sleep()
doesn’t releases the lock or monitor while waiting. wait()
is used for inter-thread communication while sleep()
is used to introduce pause on execution, generally.
Thread.sleep() sends the current thread into the “Not Runnable” state for some amount of time. The thread keeps the monitors it has acquired — i.e. if the thread is currently in a synchronized
block or method no other thread can enter this block or method. If another thread calls t.interrupt()
. it will wake up the sleeping thread.
While sleep()
is a static
method which means that it always affects the current thread (the one that is executing the sleep method). A common mistake is to call t.sleep()
where t
is a different thread; even then, it is the current thread that will sleep, not the t
thread.
User: What is the purpose of the sleep()
method in Java?
answer: The sleep()
method is used to pause the execution of the current thread for a specified amount of time. During this pause, the thread remains in the "Not Runnable" state, which means it cannot perform any operations until the specified time elapses.
User: How is wait()
different from sleep()
?
answer: The wait()
method puts the current thread into a waiting state, which won't end automatically. The thread remains in this state until another thread calls notify()
or notifyAll()
. Additionally, wait()
releases the lock or monitor it holds, while sleep()
does not.
User: Can you explain more about the locking behavior of these methods?
answer: Sure! When a thread calls sleep()
, it keeps any locks or monitors it has acquired. If the thread is inside a synchronized block or method, no other thread can enter that block or method until the sleeping thread finishes its sleep period. Conversely, when a thread calls wait()
, it releases its locks, allowing other threads to acquire them.
User: What happens if another thread interrupts a sleeping thread?
answer: If another thread calls t.interrupt()
on a sleeping thread, the sleeping thread will be woken up immediately. However, this can throw an InterruptedException
, which the sleeping thread must handle appropriately.
User: Is there a common mistake people make when using sleep()
?
answer: Yes! A common mistake is trying to call t.sleep()
where t
is a different thread. However, sleep()
always affects the currently executing thread, not the thread referenced by t
.
User: So, when should I use wait()
instead of sleep()
?
Assistant: Use wait()
for inter-thread communication, especially when you need one thread to wait for a condition to be met by another thread. On the other hand, use sleep()
when you want to introduce a delay in the execution of a thread without needing to communicate with other threads.