This chapter covers the following topics:
1. Yield () Introduction
2. Yield () example
3. Comparison of yield () and wait ()
1. Yield () Introduction
The role of yield () is to give in. It allows the current thread to move from "Run state" to "ready state", this allows other waiting threads with the same priority to get execution rights, but there is no guarantee that after the current thread calls yield (), other threads with the same priority will be able to execute; it may also be that the current thread has entered the Run status "continue running!"
2. Yield () example
Below, use the example to see how it is used.
Copy Code code as follows:
Yieldtest.java's source code
Class Threada extends thread{
Public Threada (String name) {
Super (name);
}
Public synchronized void Run () {
for (int i=0 i <10; i++) {
System.out.printf ("%s [%d]:%d\n", This.getname (), this.getpriority (), i);
I divide 4 o'clock, call yield
if (i%4 = 0)
Thread.yield ();
}
}
}
public class yieldtest{
public static void Main (string[] args) {
Threada T1 = new Threada ("T1");
Threada t2 = new Threada ("T2");
T1.start ();
T2.start ();
}
}
(of one) run result:
Copy Code code as follows:
T1 [5]:0
T2 [5]:0
T1 [5]:1
T1 [5]:2
T1 [5]:3
T1 [5]:4
T1 [5]:5
T1 [5]:6
T1 [5]:7
T1 [5]:8
T1 [5]:9
T2 [5]:1
T2 [5]:2
T2 [5]:3
T2 [5]:4
T2 [5]:5
T2 [5]:6
T2 [5]:7
T2 [5]:8
T2 [5]:9
Results show:
Thread T1 does not switch to thread T2 when it can be 4 integers. This indicates that yield () may allow the thread to enter the "Ready state" by "run State"; However, it does not necessarily give the other threads access to CPU execution (that is, other threads enter the "Run State"), even if this "other thread" has the same priority as the thread that is currently calling yield ().
3. Comparison of yield () and wait ()
As we know, the effect of Wait () is to allow the current thread to enter the standby (blocking) state while it is running, releasing the sync lock. The role of yield () is to give in, and it will leave the current thread out of the "running state." The difference between them is:
(a) Wait () is to allow a thread to go from "Run state" to "Waiting (blocking)" state, without yield () to allow the thread to enter from "Run state" to the ready state.
Wait () is a synchronous lock in which the thread releases the object it holds, and the yield () method does not release the lock.
The following example shows that yield () does not release locks.
Copy Code code as follows:
Yieldlocktest.java's source code
public class yieldlocktest{
private static Object obj = new Object ();
public static void Main (string[] args) {
Threada T1 = new Threada ("T1");
Threada t2 = new Threada ("T2");
T1.start ();
T2.start ();
}
Static class Threada extends thread{
Public Threada (String name) {
Super (name);
}
public void Run () {
Get a synchronization lock for an Obj object
Synchronized (obj) {
for (int i=0 i <10; i++) {
System.out.printf ("%s [%d]:%d\n", This.getname (), this.getpriority (), i);
I divide 4 o'clock, call yield
if (i%4 = 0)
Thread.yield ();
}
}
}
}
}
(one time) run result:
Copy Code code as follows:
T1 [5]:0
T1 [5]:1
T1 [5]:2
T1 [5]:3
T1 [5]:4
T1 [5]:5
T1 [5]:6
T1 [5]:7
T1 [5]:8
T1 [5]:9
T2 [5]:0
T2 [5]:1
T2 [5]:2
T2 [5]:3
T2 [5]:4
T2 [5]:5
T2 [5]:6
T2 [5]:7
T2 [5]:8
T2 [5]:9
Result Description:
Two threads T1 and T2 are started in main thread main. T1 and T2 in run () refer to the synchronization lock for the same object, that is, synchronized (obj). While the T1 is running, it will call Thread.yield (), but T2 will not get CPU execution power. Because, T1 does not release "the synchronization lock which obj holds"!