Importance of yield() method in Java?


A yield() method is a static method of Thread class and it can stop the currently executing thread and will give a chance to other waiting threads of the same priority. If in case there are no waiting threads or if all the waiting threads have low priority then the same thread will continue its execution.

The advantage of yield() method is to get a chance to execute other waiting threads so if our current thread takes more time to execute and allocate processor to other threads.

Syntax

public static void yield()

Example

class MyThread extends Thread {
   public void run() {
      for (int i = 0; i < 5; ++i) {
         Thread.yield(); // By calling this method, MyThread stop its execution and giving a chance to a main thread
         System.out.println("Thread started:" + Thread.currentThread().getName());
      }
      System.out.println("Thread ended:" + Thread.currentThread().getName());
   }
}
public class YieldMethodTest {
   public static void main(String[] args) {
      MyThread thread = new MyThread();
      thread.start();
      for (int i = 0; i < 5; ++i) {
         System.out.println("Thread started:" + Thread.currentThread().getName());
      }
      System.out.println("Thread ended:" + Thread.currentThread().getName());
   }
}

Output

Thread started:Thread-0
Thread started:Thread-0
Thread started:Thread-0
Thread started:Thread-0
Thread started:Thread-0
Thread started:main
Thread ended:Thread-0
Thread started:main
Thread started:main
Thread started:main
Thread started:main
Thread ended:main

Updated on: 24-Nov-2023

9K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements