Thread yield sleep Java


Summary: By the end of this tutorial "Java Thread yield sleep", you will understand the narrow difference between yield() and sleep() methods.

Thread Scheduler

Scheduling the threads to the Microprocessor for execution, one after another, is entirely the responsibility of the underlying operating system. The scheduler (controlled by OS or part of OS) considers many factors of the waiting thread while allocating Microprocessor time like waiting time, priority and nature (like daemon). Scheduler is of very complex algorithm executed number a times in a small period whenever a thread joins the pool of waiting threads or leaves (for blocked state or dead state). In this calculation and recalculation, some thread may not get a chance of Microprocessor for a considerable time. This type of thread is known as "starved thread" – starving for a long time for Microprocessor.

Programmer is not given any handle to dictate the scheduler. Then, is there any way to get a chance of processor time for a starved thread. The answer is "yield()" method.

Following is the method signature of yield() method as defined in java.lang.Thread class.

public static native void yield();

yield() is a static method that can be applied on a running thread (like static method sleep()) only. When yield is called, as Thread.yield(), the current thread (or running thread) is halted (or paused) its execution for a while (imagine, like calling sleep(1)). We know when a thread is halted its execution, again scheduler makes a fresh allocation table on the waiting threads. In this fresh table preparation, the starved thread may get a chance of processor time. It is only a chance but no compulsion.

Snippet of code on Java Thread yield sleep
public void run()
{
  for(int i = 0; i < 10; i++)
  {
    if(i == 5)
    {
      Thread.yield();
    }
}

Writing the code like this is optional and left to the discretion of the Programmer.

Differences between yield() and sleep():

  1. It differs from sleep() in that sleep() takes a long parameter and yield() carries no parameter.
  2. yield time is not in the hands of Programmer where as sleep() time is.
  3. It is advised to use sleep() instead of yield() to halt or stop the execution of a thread for a while. Blocked time is in Programmer's control with sleep() method.
  4. yield() is given to Programmer to give a chance to other threds to execute. sleep() is given to stop current thread execution.
  5. yield() method voluntarily gives up (or stops temporarily) the execution of the current thread giving chance to other threads waiting for processor's time. If no other threads are waiting or other threads have lower priority, the execution proceeds with the same thread. But sleep() method stops the execution for the specified time eventhough no other waiting threads exist.
  6. sleep() suspends the execution for a specified number of milliseconds where as yield() makes the thread to join the pool of waiting threads (or joins the queue of waiting threads at the last).

The implementation of yield() method is scheduler (or OS) dependent.

A full program on yield is available at Thread Cancellation and Starvation

2 thoughts on “Thread yield sleep Java”

  1. saravanamanikandan

    Thank you sir for the explanation. sir what is daemon thread ? when it is used in multi threading in java

Leave a Comment

Your email address will not be published.