我的原則:先會用再說,內部慢慢來
Thread.yeild
一、 作用
- Thread.yeild 線程禮讓,當前線程暫時不跑了,讓其他線程先跑。類似於你去銀行排隊辦事情,你跑到最後去重新拿個號重新排隊。
二、 注意點
- yield 跟鎖沒關係,也就是跟synchronized沒關係,也就是並不會釋放鎖。
三、 代碼Demo
public class _05_01_YieldTest implements Runnable {
@Override
public void run(){
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
for (int i = 0; i < 3; i++) {
System.out.println(Thread.currentThread() + ": " + i);
Thread.yield();
}
}
public static void main(String[] args) throws Exception {
_05_01_YieldTest runn = new _05_01_YieldTest();
Thread t1 = new Thread(runn, "t1");
Thread t2 = new Thread(runn, "t2");
t1.start();
t2.start();
Thread.sleep(100);
for (int i = 0; i < 3; i++) {
System.out.println("Main " + Thread.currentThread() + ": " + i);
Thread.yield();
}
}
}
輸出:
Thread[t1,5,main]: 0
Thread[t2,5,main]: 0
Main Thread[main,5,main]: 0
Thread[t1,5,main]: 1
Thread[t2,5,main]: 1
Main Thread[main,5,main]: 1
Thread[t2,5,main]: 2
Thread[t1,5,main]: 2
Main Thread[main,5,main]: 2
這個例子就是通過yield方法來實現三個線程的交替執行。
不過請注意:這種交替並不一定能得到保證,源碼中也對這個問題進行說明:
四、 方法簡述
/**
* 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.
意思是,給了調度器scheduler一個提示,我願意讓出當前的處理器processor給其他人,但是人家processor未必搭理你這個暗示。
*
* <p> Yield is a heuristic attempt to improve relative progression
* between threads that would otherwise over-utilise a CPU. Its use
* should be combined with detailed profiling and benchmarking to
* ensure that it actually has the desired effect.
*
* <p> It is rarely appropriate to use this method. It may be useful
* for debugging or testing purposes, where it may help to reproduce
* bugs due to race conditions. It may also be useful when designing
* concurrency control constructs such as the ones in the
* {@link java.util.concurrent.locks} package.
*/
public static native void yield();
/*
這個例子就是通過yield方法來實現兩個線程的交替執行。
不過請注意:這種交替並不一定能得到保證,源碼中也對這個問題進行說明:
主要說明了三個問題:
調度器可能會忽略該方法。
使用的時候要仔細分析和測試,確保能達到預期的效果。
很少有場景要用到該方法,主要使用的地方是調試和測試。
*/
yield方法的作用是暫停當前線程,以便其他線程有機會執行,不過不能指定暫停的時間,
並且也不能保證當前線程馬上停止。yield方法只是將Running狀態轉變爲Runnable狀態。
五、 獨角戲
public class _05_02_YieldTest {
public static void main(String[] args) throws Exception {
for (int i = 0; i < 3; i++) {
System.out.println("Main " + Thread.currentThread() + ": " + i);
Thread.yield();
}
}
}
輸出:
Main Thread[main,5,main]: 0
Main Thread[main,5,main]: 1
Main Thread[main,5,main]: 2
愛的魔力轉圈圈,後面沒其他人(Thread)了,只有你自己在排隊。
爲第六步做一個鋪墊。
六、 加個鎖 synchronized
public class _05_03_YieldTest implements Runnable {
@Override
public void run() {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized (this){
for (int i = 0; i < 3; i++) {
System.out.println(Thread.currentThread() + ": " + i);
Thread.yield();
}
}
}
public static void main(String[] args) throws Exception{
_05_03_YieldTest runn = new _05_03_YieldTest();
Thread t1 = new Thread(runn, "t1");
Thread t2 = new Thread(runn, "t2");
t1.start();
t2.start();
Thread.sleep(100);
synchronized (runn){
for (int i = 0; i < 3; i++) {
System.out.println("Main " + Thread.currentThread() + ": " + i);
Thread.yield();
}
}
}
}
輸出:
Thread[t1,5,main]: 0
Thread[t1,5,main]: 1
Thread[t1,5,main]: 2
Main Thread[main,5,main]: 0
Main Thread[main,5,main]: 1
Main Thread[main,5,main]: 2
Thread[t2,5,main]: 0
Thread[t2,5,main]: 1
Thread[t2,5,main]: 2
結論:
啥意思呢?就是說,雖然我已經 yield 妥協讓步給其他人,但是我鎖住了,你們現在也沒法用,等我跑完你們再來吧。
七、優先級Priority問題
public class _05_04_YieldTest implements Runnable {
@Override
public void run() {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
for (int i = 0; i < 3; i++) {
System.out.println(Thread.currentThread() + ": " + i);
Thread.yield();
}
}
public static void main(String[] args) throws Exception {
_05_04_YieldTest runn = new _05_04_YieldTest();
Thread t1 = new Thread(runn, "t1");
Thread t2 = new Thread(runn, "t2");
Thread t3 = new Thread(runn, "t3");
t1.setPriority(Thread.MAX_PRIORITY);
t3.setPriority(Thread.MIN_PRIORITY);
t1.start();
t2.start();
t3.start();
}
}
輸出:
Thread[t3,1,main]: 0
Thread[t1,10,main]: 0
Thread[t2,5,main]: 0
Thread[t3,1,main]: 1
Thread[t1,10,main]: 1
Thread[t2,5,main]: 1
Thread[t3,1,main]: 2
Thread[t1,10,main]: 2
Thread[t2,5,main]: 2
你看優先級priority 最低的 t3 居然最先跑。
結論: priority 不起作用。
八、實戰結論
- yield 能夠讓線程交替進行 (_05_01_YieldTest)
- yield 如果只有一個線程,那麼會繼續運行 ( _05_02_YieldTest)
- yield 方法和同步沒關係,也就是和ObjectMonitor沒關係,你硬上鎖就是在唱獨角戲 ( _05_03_YieldTest)
- 優先級priority 不生效。(_05_04_YieldTest)
九、JVM源碼
十、 番外篇
上一章節:【線程】 Thread.sleep 與 Object.wait 的區別 (三)
下一章節:【線程】 Thread.sleep 與 Thread.yield 的區別 (五)