Learn the differences between sleep() and wait() methods in Java. Learn when to use which method and what effect they bring in Java concurrency.
1. Java sleep() and wait() – Discussion
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.
Read more : Working with wait() and notify()
2. Java sleep() and wait() – Example
synchronized(LOCK) { Thread.sleep(1000); // LOCK is held }
synchronized(LOCK) { LOCK.wait(); // LOCK is not held }
Read more : Difference between yield() and join()
3. Java sleep() vs wait() – Summary
Let categorize all above points in short to remember.
3.1. Method called on
wait()
– Call on an object; current thread must synchronize on the lock object.sleep()
– Call on a Thread; always currently executing thread.
3.2. Synchronized
wait()
– when synchronized multiple threads access same Object one by one.sleep()
– when synchronized multiple threads wait for sleep over of sleeping thread.
3.3. Lock duration
wait()
– release the lock for other objects to have chance to execute.sleep()
– keep lock for at least t times if timeout specified or somebody interrupt.
3.4. wake up condition
wait()
– until call notify(), notifyAll() from objectsleep()
– until at least time expire or call interrupt().
3.5. Usage
sleep()
– for time-synchronizationwait()
– for multi-thread-synchronization.
Hope above information will add some value in your knowledge-base.
Happy Learning !!
Thread sleep method Java doc
Object wait() method Java doc
which one hits a performance ? sleep or wait ?
Awesome explanation. Thanks keep up the good work and knowledge sharing.
it’s true
Great tutorial, thanks!
Very informative article. Thanks
very informative one…thank you
Good Article..Really useful
Hi Lokesh,
many thanks for your valuable posts .
The only site I refer for any java queries is this !!!!!!!!!!
Hi Lokesh,
Nice article. Please explain/add yield() and join() method to this article as sometime this 4 api looks similar yet so diffrent.
I added it to my TODO list.
I have found the article very clear and explanatory.
you write in easy way…really you are doing good job ..james gosling invented java and lokesh is making easyjava 🙂
🙂
Hi.. I have a question!!!
Why wait(), notift(),notifyAll() always works in Synchronized Block?
Puneet, because it was designed in this way only. These methods were introduced as means of making safe interactions between threads. If they were allowed without synchronization, there were occur lots of other problems such as race condition, data corruption and even program crash.
Please note that a program does not get any kind of surety from low level OS thread scheduling, so it depends on language runtime to manage its thread.
Thanks for the answer Lokesh..
I have 1 more question!!!
why wait(), notift(),notifyAll() are the methods of Object class, insteed of Thread Class?
Pardon for being lazy here, just came from office. You can get a good explanation here:
https://stackoverflow.com/questions/17840397/concept-behind-putting-wait-notify-methods-in-object-class
It is so because, Thread 2 will not have the idea of Thread 1 state, where t1 is in wait state or it is notified. That’s why there methods are of Object class. please let me know, if i am wrong.
I will say partly because real problem is to find out which all threads are in application and then which one are waiting on current monitor.
Lokesh, What happens if someone dosent call notify( earlier thread called wait) ?
will this keep the program in hanged state? will it not allow program to exit/terminate?
Tricky question.. In my view, no major impact on application just because one of several hundreds thread is blocked somewhere. Rest of application will run fine. This is very much clear and easy.
No, program will not hang. According to theory, it should not allow to terminate and you might need to do it forcefully.
Good points you are covering here, it will be more helpful if you take up the relatively new topics like concurrent package, annotations, generics, nio etc
Sure, I will take up these topics in near future..