Difference between Thread.yield and Thread.sleep in Java? Answer

Sleep vs yield in Java
Sleep and yield are two methods that are used to get CPU back from Thread to Thread Scheduler in java but they are completely different than each other. The major difference between Sleep vs yield is that sleep is more reliable than yield and it's advised to use sleep(1) instead of yield to relinquish CPU in multi-threaded Java applications to give an opportunity to other threads to execute. In this Java tutorial, we will what are the differences between yield and sleep in Java. But before seeing the difference between sleep and Yield let's see some similarities between yield and sleep in Java



Similarities between Thrad's Sleep and yield in Java

 Here are common things between sleep and yield method of Thread class in Java programming :

1. Location
Both yield and sleep are declared on java.lang.Thread class. Yes, unlike wait() and notify() which are defined in java.lang.Object class, both sleep() and yield() method is defined on java.lang.Thread class. 


2. Static Methods
Both sleep() and yield() are static methods and operate on the current thread. It doesn't matter which thread's object you used to call this method, both these methods will always operate on the current thread.


3. Impact
Both Sleep as well as Yield is used to relinquish CPU from the current thread, but at the same time it doesn't release any lock held by the thread. If you also want to release locks along with releasing CPU, you should be using the wait() method instead. See the difference between the sleep() and wait() method for more details. 

Now let's see what are differences between Sleep and Yield in Java and what are best practices to use sleep and yield in Java multi-threaded program:




Difference between sleep and yield in Java

Here is a list of some of the key differences between the sleep and yield method of Java Multithreading package and API

1. Purpose
The Thread.sleep() method is overloaded in Java as sleep(long milliseond) and sleep(long millis, int nanos) . the former version of sleep will stop the current thread for a specified millisecond while the later version of sleep allows specifying sleep duration till nanosecond. 

Thread.sleep() will cause the currently executing thread to stop execution and relinquish the CPU to allow the Thread scheduler ot allocate CPU to another thread or same thread depends upon the Thread scheduler. Thread.yield() is also used to relinquish CPU but the behavior of sleep() is more determined than yield across platforms. Thread.sleep(1) is a better option than calling Thread.yield for the same purpose.


2. Release of Monitor lock
The Thread.sleep() method doesn't cause the currently executing thread to give up any monitors while sleeping.


3. InterruptedExceptoin
Thread.sleep() method throws InterruptedExcepiton if another thread interrupts the sleeping thread, this is not the case with the yield method. You can also check these Java thread courses for experienced developers to learn more about essential multithreading concepts. 

Difference between yield and sleep in Java Thread? Answer



That's all on the difference between the Sleep and Yield method in the Java thread. In summary, prefer the sleep() over yield() method to relinquish CPU if you need to. Remember the purpose of the sleep() method is to pause the current thread, but it will not release any lock held by the current thread. If you also want the thread to release CPU as well as any lock held, consider using the wait() method instead.

Further Learning
Java Multithreading and Concurrency Courses

2 comments:

  1. thanks, i did read some in similar in effective java 2nd edition. Then we can say that use thread.yield has no sense, or what situations can i use thread.yield ?

    ReplyDelete
    Replies
    1. @Junca, to be honest I have not seen real uses of Thread.yield(), in theory it did say that it will force Thread to relinquish CPU but there is no control who will get it.

      Delete

Feel free to comment, ask questions if you have any doubt.