The contents involved in this chapter include:
1. Introduction to yield()
2. yield() example
3. Comparison between yield() and wait()
1. Introduction to yield()
The purpose of yield() is to give in. It allows the current thread to enter the "ready state" from the "running state", so that other waiting threads with the same priority can obtain execution rights; however, it does not guarantee that after the current thread calls yield(), others have the same priority. The thread will definitely obtain execution rights; it is also possible that the current thread enters the "running state" and continues to run!
2. yield() example
Below, see its usage by example.
The code copy is as follows:
// YieldTest.java 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);
// When i divides 4, 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();
}
}
(One time) operation result:
The code copy is 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 description:
When "thread t1" can be integered by 4, it does not switch to "thread t2". This shows that although yield() can allow threads to enter the "ready state" from "running state"; however, it does not necessarily allow other threads to obtain CPU execution rights (i.e., other threads enter the "running state"), even if this "Other threads" have the same priority as the thread currently calling yield().
3. Comparison between yield() and wait()
We know that the function of wait() is to allow the current thread to enter the "wait (blocking) state from the "running state" and also release the synchronization lock. The function of yield() is to give in, which will also cause the current thread to leave the "running state". Their differences are:
(01) wait() is to let the thread enter the "wait (blocking) state" from the "running state", while not yield() is to let the thread enter the "ready state" from the "running state".
(02) wait() is a synchronization lock that will thread release the object it holds, while the yield() method will not release the lock.
The following example shows that yield() will not release the lock.
The code copy is as follows:
// YieldLockTest.java 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 the synchronization lock of the obj object
synchronized (obj) {
for(int i=0; i <10; i++){
System.out.printf("%s [%d]:%d/n", this.getName(), this.getPriority(), i);
// When i divides 4, call yield
if (i%4 == 0)
Thread.yield();
}
}
}
}
}
(One time) result:
The code copy is 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
Results description:
Two threads t1 and t2 are started in the main thread main. t1 and t2 will refer to the synchronization lock of the same object in run(), that is, synchronized(obj). During the t1 operation, although it will call Thread.yield(); t2 will not obtain CPU execution rights. Because t1 does not release the "synchronous lock held by obj"!