Close

Java - Yielding Thread Execution

[Last Updated: Feb 7, 2017]

The static method Thread#yield() causes the current thread to yield its current use of a processor.

In other words the current thread may stop executing momentarily to give chance to other competing threads having the same priorities.

Let's see that with an example.


Threads without Yield

public class ThreadYieldExample {
    public static void main (String[] args) {
        Task task1 = new Task();
        new Thread(task1).start();

        Task task2 = new Task();
        new Thread(task2).start();
    }

    private static class Task implements Runnable {
        private int c;

        @Override
        public void run () {
            String threadName = Thread.currentThread()
                                      .getName();

            System.out.println(threadName + " started.");
            for (int i = 0; i < 1000; i++) {
                c++;
            }

            System.out.println(threadName + " ended.");
        }
    }
}/

In above example we are invoking two threads simultaneously with same priories.

Both threads performing the same task

The output is not predictable i.e. we cannot guess which thread is going to take more CPU time and hence faster than the other.

Running above code multiple times:

Thread-0 started.
Thread-1 started.
Thread-0 ended.
Thread-1 ended.

OR

Thread-1 started.
Thread-0 started.
Thread-1 ended.
Thread-0 ended.

Threads with Yield

Let's modify our above code to use yield method for one of the thread:

public class ThreadYieldExample {
    public static void main (String[] args) {
        Task task1 = new Task(true);
        new Thread(task1).start();

        Task task2 = new Task(false);
        new Thread(task2).start();

    }

    private static class Task implements Runnable {
        private final boolean shouldYield;
        private int c;

        public Task(boolean shouldYield){
            this.shouldYield = shouldYield;
        }
        @Override
        public void run () {
            String threadName = Thread.currentThread()
                                      .getName();

            System.out.println(threadName + " started.");
            for (int i = 0; i < 1000; i++) {
                c++;
                if(shouldYield){
                    Thread.yield();
                }
            }

            System.out.println(threadName + " ended.");
        }
    }
}

In above example we are yielding the first thread in favor of other. As a result the first thread spends less CPU time and it becomes slower than the other, hence 'Thread-0 ended.' printed at the end all the time

Output

Thread-0 started.
Thread-1 started.
Thread-1 ended.
Thread-0 ended.

Just like thread priorities, yield method behavior varies from OS to OS. We shouldn't make coding decisions based on this method. Also this is only a hint, the thread scheduler (on OS level) is free to ignore this hint.


Example Project

Dependencies and Technologies Used:

  • JDK 1.8
  • Maven 3.0.4

Java Thread Yield Example Select All Download
  • java-thread-yield
    • src
      • main
        • java
          • com
            • logicbig
              • example
                • ThreadYieldExample.java

    See Also