The Thread.yield () method acts: Pauses the currently executing thread and executes other threads. (may not be effective)
Yield () gives the currently running thread back to the operational state to allow other threads with the same priority to get the chance to run. Therefore, the purpose of using yield () is to allow the appropriate rotation between threads with the same priority to execute . However, in practice there is no guarantee that yield () can achieve the purpose of concession, because the thread that withdraws may be checked again by the thread scheduler.
Conclusion: In most cases, yield () will cause the thread to go from the running state to the operational state, but it may not be effective.
public class Testyield {
public static void Main (String [] args) {
MyThread T1 = new MyThread ("T1");
MyThread t2 = new MyThread ("T2");
T1.start ();
T2.start ();
}
}
Class MyThread extends thread{
MyThread (String s) {
Super (s);
}
public void Run () {
for (int i = 0; I <=; i + +) {
System.out.println (GetName () + ":" +i);
if (("T1"). Equals (GetName ())) {
if (i = = 0) {
Yield ();
}
}
}
}
}
The result of the operation is variable.
Usage of yield () in Java threads