yield() is a native method its implementation in java 6 has been changed as compared to its implementation java 5. As method is native it’s implementation is provided by JVM.
In java 5, yield() method internally used to call sleep() method giving all the other threads of same or higher priority to execute before yielded thread by leaving allocated CPU for time gap of 15 millisec.
But java 6, calling yield() method gives a hint to the thread scheduler that the current thread is willing to yield its current use of a processor. The thread scheduler is free to ignore this hint. So, sometimes even after using yield() method, you may not notice any difference in output.
8 salient features of yield() method >
- Definition : yield() method when called on thread gives a hint to the thread scheduler that the current thread is willing to yield its current use of a processor. The thread scheduler is free to ignore this hint.
- Thread state : when yield() method is called on thread it goes from running to runnable state, not in waiting state. Thread is eligible to run but not running and could be picked by scheduler at the discretion of the implementation.
- Waiting time : yield() method stops thread for unpredictable time.
- Static method : yield() is a static method, hence calling Thread.yield() causes currently executing thread to yield.
- Native method : implementation of yield() method is provided by JVM.
Let’s see definition of yield() method as given in java.lang.Thread -
public static native void yield();
|
- synchronized block : thread need not to to acquire object lock before calling yield() method i.e. yield() method can be called from outside synchronized block.
- Belongs to which class : yield() method belongs to java.lang.Thread class.
Program to show usage of yield() method>
calling yield() method gives a hint to the thread scheduler that the current thread is willing to yield its current use of a processor, but thread scheduler may ignore it and in that case yield() won’t won’t have any impact on output of program.
class MyRunnable1 implements Runnable{
public void run(){
for(int i=0;i<5;i++){
Thread.yield();
System.out.println("i="+i+" ,ThreadName="+Thread.currentThread().getName());
}
}
}
class MyRunnable2 implements Runnable{
public void run(){
for(int i=0;i<5;i++){
System.out.println("i="+i+" ,ThreadName="+Thread.currentThread().getName());
}
}
}
/** Copyright (c), AnkitMittal JavaMadeSoEasy.com */
public class YieldTest {
public static void main(String...args){
Thread thread1=new Thread(new MyRunnable1(),"Thread-1");
Thread thread2=new Thread(new MyRunnable2(),"Thread-2");
thread1.start();
thread2.start();
}
}
/*OUTPUT
i=0 ,ThreadName=Thread-1
i=0 ,ThreadName=Thread-2
i=1 ,ThreadName=Thread-1
i=1 ,ThreadName=Thread-2
i=2 ,ThreadName=Thread-1
i=2 ,ThreadName=Thread-2
i=3 ,ThreadName=Thread-1
i=3 ,ThreadName=Thread-2
i=4 ,ThreadName=Thread-1
i=4 ,ThreadName=Thread-2
*/
|
We may even notice that, if program is executed with yield() method even than output of program is same as that of program executed without yield() method.
RELATED LINKS>
Volatile keyword vs synchronized>
Volatile keyword in java- difference between synchronized and volatile, 10 key points about volatile keyword, why volatile variables are not cached in memory
Differences between synchronized and volatile keyword in detail
Race Condition >
Race condition in multithreading and it's solution
Consumer Producer problem solution using different techniques >
Solve Consumer Producer problem by using wait() and notify() methods in multithreading
solve Consumer Producer problem by using wait() and notify() methods, where consumer can consume only when production is over
How to solve Consumer Producer problem without using wait() and notify() methods, where consumer can consume only when production is over.
Important Thread methods (salient features, usage with programs)>
Join() method - ensure all threads that started from main must end in order in which they started and also main should end in last. Types of join() method-10 salient features of join
Sleep() method in threads - 10 key features with programs
Yield() method in threads - 8 key features with programs
Wait() and notify() methods- Definition, 8 key features, solving consumer producer problem with & without these methods and consequences of not using wait() and notify() methods.
Daemon threads - 12 salient features of Daemon Thread
2 alternate ways to stop thread, as stop() method is deprecated
Using Suspend and resume method in threads
Differences between important thread methods >
Difference between wait() and sleep() method in threads
Differences and similarities between yield() and sleep() in threads
Difference between notify() and notifyAll() methods, with program
Difference between wait() and wait(long timeout) -What will be Thread states
Guidelines to threadsafe code >
Guidelines to thread safe code, most important point we must take care of in multithreading programs
Interviews >