Yield Method in Java | Yield in Java

Yield() method in Java | When a currently executing thread goes to the runnable state from running state, this process is called yield in Java.

When running state receives higher priority thread than the thread that is currently running, thread scheduler sends the currently running thread back to the runnable state and selects another thread of equal or higher priority to start its execution.

Look at the below figure to understand better.Life cycle of thread when yield method in java is called

To send a running thread back to runnable state, yield() method of Thread class is called on the current Thread object. The general syntax to call yield() method in Java program is as follows:

Syntax:

static void yield()

Since yield() method is a static method so we can call it by using its class name like this:

Thread.yield();

In the preceding syntax, yield() method is called on the Thread object. Calling yield() method on the Thread object pauses execution of the current thread and selects thread of equal or higher priority to start its execution.

Program based on yield() method in Java


Let’s create a program where we will move back current executing thread to the runnable state from running state. Look at the following source code.

Program source code 1:

public class A implements Runnable
{
public void run()
{
System.out.println(Thread.currentThread()); 
Thread.yield(); // Calling yield() method on current thread to move back into the runnable state from running state.

System.out.println(Thread.currentThread());
 }
public static void main(String[] args) 
{
A a1 = new A();
Thread t1 = new Thread(a1, "First Child Thread");

A a2 = new A();
Thread t2 = new Thread(a2, "Second Child Thread");

t1.start();
t2.start();
 }
}
Output:
      Thread[First Child Thread,5,main]
      Thread[Second Child Thread,5,main]
      Thread[First Child Thread,5,main]
      Thread[Second Child Thread,5,main]

Explanation:

1. As you can observe in the output, first t1 starts running. When yield() method is invoked, thread t1 goes to the runnable state from running state, and thread t2 starts execution. Then t2 goes into the runnable state because yield() method is called on it.


2. Now, t1 resumes its execution. After complete execution of t1, t1 exits and terminated. Then t2 resumes for the execution.

Let’s take another program where we will set the priority and then we will call yield() method. Look at the source code.

Program source code 2:

public class A implements Runnable
{
public void run()
{
System.out.println(Thread.currentThread()); 
Thread.yield(); // Calling yield() method on current thread to move back into the runnable state from running state.

System.out.println(Thread.currentThread());
 }
public static void main(String[] args) 
{
A a1 = new A();
Thread t1 = new Thread(a1, "First Child Thread");

A a2 = new A();
Thread t2 = new Thread(a2, "Second Child Thread");

t1.setPriority(4); // Setting the priority of thread
t2.setPriority(8);

t1.start();
t2.start();
 }
}
Output:
       Thread[Second Child Thread,8,main]
       Thread[First Child Thread,4,main]
       Thread[Second Child Thread,8,main]
       Thread[First Child Thread,4,main]

Explanation:

1. Priority of thread t1 is set to 4 whereas priority of thread t2 is set to 8.
2. Since thread t2 has the highest priority so thread scheduler selects it for execution. When yield() method is called on thread t2, t2 goes to the ready state.

3. Now scheduler selects thread t1 for its execution. When yield() method is invoked, t1 goes to the ready state.
4. Since thread t2 enjoys the highest priority, so scheduler again selects it for execution.
5. After the complete execution of t2, thread t2 terminated, and then t1 starts execution

Hope that this tutorial has covered important points related to yield() method in Java with example programs. Keep in mind the following important points.

Key Points to Remember

1. Do not use yield() method constantly in Java program.
2. When the yield() method is called on a thread object, it does not move the thread into sleeping, waiting, or blocking state. It sends thread into runnable state that can be resumed to the running state later.
Next ⇒ Java Thread Join⇐ PrevNext ⇒