Difference between Wait, Sleep and Yield in Java

All the three functions wait, sleep and yield in java are related to threads but all of them do not belong to thread class. It is often confusing to distinguish between these three. Here is a quick difference between wait, sleep and yield functions:


Wait Sleep Yield
1. Method of class Object Method of class Thread Method of class Thread
2. Non – Static Static Static
3. Not invoked on a thread instance Invoked on a thread instance Invoked on a thread instance
4. Moves the thread to “wait’ state Moves the thread to “wait” state Moves the thread to “ready” state
5. Thread loose the ownership of the lock Thread doesn’t loose the ownership of the lock Thread doesn’t loose the ownership of the lock

Thus one thing that is common to all the three functions is that they change the state of thread.

Please let us know for any queries or feedback in the comments sections below.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">