Yield() Method in Java

Learn via video course
FREE
View all courses
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Topics Covered

Overview

The yield() method is used in Java to hint the task scheduler to move the currently executing task to the Ready state and move another task or thread to the running state. The task scheduler is free to ignore the hint. Through the yield() method, a thread is willing to yield or relinquish its current use of the processor.

Syntax of Yield() in Java

The general definition of yield function in Java is as follows:

Because the yield() method is a static function, it can be called with its class name as shown below

Here, we call the yield() method on the Thread object. Calling the yield() method on the Thread object pauses the currently executing thread and puts it in Ready state and selects another runnable thread from the ready state to start its execution.

Parameters of Yield() in Java

yield() method doesn't accept any parameters and is called on the Thread object. The execution of a thread on which yield() is called stops to execute another thread from the ready state.

Return Values of Yield() in Java

The yield() method doesn't return any value when it is called on the Thread object.

Example

Let us see an example where we will move the currently executing thread back to the ready state from the running state.

Output:

Explanation:

  • Here, we create two threads, t1 and t2, that execute the run() method in YieldDemo Class.
  • As we can observe from the output, thread t1 start executing first. When the yield() method is invoked, thread t1 goes to the Ready state from the Running state, and thread t2 starts executing.
  • Then t2 goes in Ready state because yield() is called on it.
  • Now, thread t1 resumes its execution and terminates. The execution of the second thread follows this because it was in a Ready state.

Let us see another example where we are assigning priorities to the threads.

Output:

Explanation:

  • Here, the priority of thread t1 is 4, whereas the priority of the second thread t2 is 8. Because the second thread has the highest priority so thread selector executes it first.
  • When the yield() method is called on thread t2, t2 returns to the Ready state. Now, the selector selects thread t1 for its execution.
  • When yield() is called on t1 because t2 has the highest priority, so t2 executes again, and after its execution finishes, thread t1 resumes its execution.

Yield() Method in Java

Let us suppose we have three threads, T1, T2 and T3, that need to be executed.

Thread T1 gets the processor and starts executing while the remaining threads T2 and T3 are in the Ready state. The time required for thread T1 is 4 hours, while threads T2 and T3 take only 2 and 4 minutes, respectively. This means threads T1 and T2 need to wait for four hours to complete a total of 6-minute jobs. In such scenarios where one thread takes too long for its completion, we need to have a method that allows the execution of other threads in between if something important is pending. Java has a yield() method to provide such functionality.

yield method in java diagram

When the current executing state receives a job with higher priority than the current executing thread and the current executing thread is willing to yield its use of the processor, the thread scheduler stops current thread and sends it to Ready state and selects different thread with equal or higher priority task to start its execution. The yield() method in the Thread class in Java is called on the current Thread object to send the current running thread back to the ready state.

Use of Yield Method

  • It may be useful for debugging or testing purposes, where it may help to reproduce bugs due to race conditions.
  • It can be used when creating concurrency control constructs, such as those in the java.util.concurrent.locks package.

Difference between Wait, Sleep, Join and Yield in Java

Let us see a comparison between yield(), join() and sleep() methods.

Propertyyield()join()sleep()
PurposeWe use yield(), if a thread is willing to pass its execution (set its status back to Ready state) to give a chance to other remaining threads in the ready state.We use join() method when a thread is willing to wait until the completion of a different thread.sleep() method is used if a thread does not want to perform operation for a definite amount of time.
Is it overloaded?NoYesYes
Is it final?NoYesNo
Static method?YesNoYes
Throws exception?NoYesYes

Conclusion

  • yield() method is defined in Thread.java. It hints to the scheduler that the current thread is ready to yield or relinquish its use of the processor and the scheduler is free to ignore the hint.
  • If the thread calls the yield() method, the scheduler checks if there is any thread on the ready state waiting to be executed, it moves the current thread to the Ready state and gives the processor another task.
  • yield() method is called on the Thread object and accepts no parameter and does not return any output.