In this post we’ll see the difference between sleep()
and yield()
methods in Java. Since both of these methods sleep()
and yield() cause the currently executing thread to suspend its execution some users find it confusing to differentiate
between sleep and wait methods. So let’s try to see the difference between sleep() and yield() methods
in Java.
sleep() and yield() methods in Java
As already stated both of these methods are used to suspend the execution of the current thread but sleep() method is more apt to be used for that purpose. What tilts it in favor of sleep() method is the fact that it will actually cause the currently executing thread to sleep for a specified period of time where as yield() is just a hint to the scheduler that the current thread is willing to yield its current use of a processor. The scheduler can ignore this hint too. That makes yield() method non-deterministic.
Even the Java doc says something similar-
It is rarely appropriate to use yield() method. It may be useful for debugging or testing purposes, where it may help to reproduce bugs due to race conditions.
Reference- https://docs.oracle.com/javase/10/docs/api/java/lang/Thread.html#yield()
Looking at both of these methods you will find a lots of similarities though-
- Both yield() and sleep() methods are member of the java.lang.thread class and both of them are static methods
- Both of these methods work on the currently executing thread. So calling Thread.sleep() or Thread.yield() method will suspend the execution of the currently executing thread.
- Both yield() and sleep() methods can be used in any context, unlike wait() method there is no compulsion to be called with in a synchronized context. Even if these methods are called from a synchronized block or method the thread doesn’t release the object’s lock as required in case of wait() method.
sleep() Vs yield() in Java
Now when you have seen the general description of sleep() and yield() methods, even seen the similarities between them it is time to go through the difference between sleep and yield methods in Java multi-threading.
- sleep() method takes duration as argument and ensures that the current thread suspends its execution for the specified time.
yield() method is merely a hint to the scheduler that the current thread is willing to yield its current use of a processor. Scheduler can even ignore this hint to yield. - When sleep() method is called current thread will definitely go to sleep for the specified time.
In case of yield() method first of all it is just a hint which can be ignored, even if the current thread yields the CPU it can start running again if there is no other thread with the same or more thread priority. - sleep() method throws InterruptedException if sleeping thread is interrupted so call to sleep() method should either be enclosed in try-catch block or it should be declared using throws clause.
yield() method doesn’t throw InterruptedException.
yield method Java example
public class TestThread implements Runnable { @Override public void run() { System.out.println(Thread.currentThread().getName() + " Entering run method"); for(int i = 0; i < 3; i++){ System.out.println(Thread.currentThread().getName() + " Value - " + i); if(i == 1){ Thread.yield(); } } } public static void main(String[] args) { TestThread obj = new TestThread(); Thread t1 = new Thread(obj); Thread t2 = new Thread(obj); Thread t3 = new Thread(obj); //long startTime = System.currentTimeMillis(); t1.start(); t2.start(); t3.start(); } }Output
Thread-1 Entering run method Thread-2 Entering run method Thread-2 Value - 0 Thread-2 Value - 1 Thread-0 Entering run method Thread-0 Value - 0 Thread-0 Value - 1 Thread-1 Value - 0 Thread-1 Value - 1 Thread-2 Value - 2 Thread-0 Value - 2 Thread-1 Value - 2
As you can see from the output each thread is yielding the CPU when value of i is 1 giving another thread chance to execute.
That's all for the topic Difference Between sleep() And yield() Methods in Java. If something is missing or you have something to share about the topic please write a comment.
You may also like
No comments:
Post a Comment