Java - Thread Thread Yield

Introduction

A thread may give up the CPU by calling the static yield() method of the Thread class.

yield() method hints the thread scheduler that it may pause the running thread and give the CPU to other threads.

It is just a hint to the scheduler and is not guaranteed to give a consistent result across different platforms.

A thread that calls the yield() method continues to hold the monitor locks.

Demo

public class Main extends Thread {
  public void run() {
    System.out.println("Thread started...");

    Thread.yield();//  www. j  a v a 2 s.com

    System.out.println("Thread stopped...");
  }

  public static void main(String[] args) {
    Main vv = new Main();
    vv.run();
  }
}

Result