How to Implement thread.yield() in Java: Examples

Last updated on Jul 21,2020 5.1K Views

How to Implement thread.yield() in Java: Examples

edureka.co

Theoretically, to ‘yield’ means to let go, to give up, to surrender. A yielding thread tells the virtual machine that it’s willing to let other threads be scheduled in its place. This indicates that it’s not doing something too critical. Note that it’s only a hint, though, and not guaranteed to have any effect at all. In this thread.yield() in Java, we will cover the following topics:

 

Introduction to thread.yield() in Java

yield() is defined as following in Thread.java. A hint to the scheduler that the current thread is willing to yield its current use of a processor. The scheduler is free to ignore this hint.

Yield is a heuristic attempt to improve relative progression between threads that would otherwise over-utilize a CPU. Its use should be combined with detailed profiling and benchmarking to ensure that it actually has the desired effect.

Syntax:

public static native void yield();

 

Difference between wait, sleep, join and yield in Java


 
yield() vs wait()

 yield() vs sleep()

 yield() vs join()

 

Example of thread.yield() in Java

public class JavaYieldExp extends Thread  
{  
public void run()  
{  
        for (int i=0; i<3 ; i++)  
            System.out.println(Thread.currentThread().getName() + " in control");  
    }  
    public static void main(String[]args)  
    {  
        JavaYieldExp t1 = new JavaYieldExp();  
        JavaYieldExp t2 = new JavaYieldExp();  
        // this will call run() method  
        t1.start();  
        t2.start();  
        for (int i=0; i<3; i++)  
        {  
            // Control passes to child thread  
            t1.yield();  
            System.out.println(Thread.currentThread().getName() + " in control");  
        }  
    }  
}  

Output:

 

The next example shows the usage of java.lang.Thread.yield() method:

import java.lang.*;
public class ThreadDemo implements Runnable {
   Thread t;
   ThreadDemo(String str) {
      t = new Thread(this, str);
      // this will call run() function
      t.start();
   }
   public void run() {
      for (int i = 0; i < 5; i++) {
         // yields control to another thread every 5 iterations
         if ((i % 5) == 0) {
            System.out.println(Thread.currentThread().getName() + "
            yielding control...");
            /* causes the currently executing thread object to temporarily 
            pause and allow other threads to execute */
            Thread.yield();
         }
      }
      System.out.println(Thread.currentThread().getName() + " has finished executing.");
   }
   public static void main(String[] args) {
      new ThreadDemo("Thread 1");
      new ThreadDemo("Thread 2");
      new ThreadDemo("Thread 3");
   }
}

Output:

With this, we have come to the end of our article. I hope you understood how the thread.yield() in Java works and how it is used the programming language.

Check out the Java training by Edureka, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe. Edureka’s Java J2EE and SOA training and certification course is designed for students and professionals who want to be a Java Developer. The course is designed to give you a head start into Java programming and train you for both core and advanced Java concepts along with various Java frameworks like Hibernate & Spring.

Got a question for us? Please mention it in the comments section of this “Thread.yield() in Java” blog and we will get back to you as soon as possible.

Upcoming Batches For Java Course Online
Course NameDateDetails
Java Course Online

Class Starts on 7th December,2024

7th December

SAT&SUN (Weekend Batch)
View Details
BROWSE COURSES