Difference between wait(), sleep() and yield() in Java
While all of these methods can be used to put a thread into sleep or take away the CPU but what is the difference between them.
Hello guys, the difference between wait and sleep or the difference between sleep and yield in Java is one of the popular core Java interview questions and most asked on Java developer interviews with 2 to 5 years of experience.
Out of three methods that can be used to pause a thread in Java, sleep()
and yield()
methods are defined in thread class while wait() is defined in the Object class, which is another interview question.
In the past few posts, I have shared popular Java interview questions like How ConcurrentHashMap work in Java? Why String is Immutable, difference between List, List<Object> and ReentrantLock vs synchronized in Java and in this article, I am going to share another popular Java multithreading interview question.
The key difference between wait()
and sleep()
is that the former is used for inter-thread communication while later is used to introduced to pause the current thread for a short duration.
This difference is more obvious from the fact that, when a thread calls the wait() method, it releases the monitor or lock it was holding on that object, but when a thread calls the sleep()
method, it never releases the monitor even if it is holding.
Coming back to yield(), it's little different than wait()
and sleep()
, it just releases the CPU hold by Thread to give another thread an opportunity to run though it's not guaranteed who will get the CPU.
It totally depends upon thread scheduler and it's even possible that the thread which calls the yield() method gets the CPU again. Hence, it's not reliable to depend upon the yield() method, it's just on the best effort basis.
Let's dive into the comparison of wait, sleep, and yield to gain insights into which approach best suits your concurrent programming needs.
Btw, if you are a complete beginner to Java threads, I strongly suggest you join a hands-on multithreading course like Multithreading and Parallel Computing in Java from Udemy. It's a great course to learn thread basics It's also very affordable and you can get in just $10on Udemy sales.
New Year Offer - 35% Discount
I would like you to thank you for reading Javarevisited. As a token of our appreciation, we're offering you a limited-time offer of 35% off a paid subscription.
Instead of paying 50$ / year, you pay 32.5$ / year (only 3$ / month)!
Here are the benefits you unlock with a paid subscription:
Get access to paid subscribers posts. 📗
Access to Full archive of more than 120+ posts 🏆
Many expense it with their team's learning budget
In this Java tutorial, we will learn what is sleep in Java, important points of sleep in java and the difference between Wait and sleep in Java.
Difference between Wait and Sleep in Java
The main difference between wait and sleep is that wait() method releases the acquired monitor when the thread is waiting while Thread.sleep() method keeps the lock or monitor even if the thread is waiting.
Keep reading with a 7-day free trial
Subscribe to Javarevisited Newsletter to keep reading this post and get 7 days of free access to the full post archives.