Difference Between sleep() And yield() Methods in Java

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.

Referencehttps://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-

  1. Both yield() and sleep() methods are member of the java.lang.thread class and both of them are static methods
  2. 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.
  3. 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.

  1. 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.
  2. 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.
  3. 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

Output

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

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.