面試官:為什么 wait() 方法需要寫在while里、而不是if? 我回答不上來
? ? ?
? ?正文? ?
問:為什么是 while 而不是 if ?
大多數(shù)人都知道常見的使用 synchronized 代碼:
synchronized?(obj)?{
?????while?(check?pass)?{
????????wait();
????}
????//?do?your?business
}
那么問題是為啥這里是 while 而不是 if 呢?這個(gè)問題我最開始也想了很久,按理來說已經(jīng)在 synchronized 塊里面了嘛,就不需要了。這個(gè)也是我前面一直是這么認(rèn)為的,直到最近看了一個(gè) Stackoverflow 上的問題才對(duì)這個(gè)問題有了比較深入的理解。
試想我們要試想一個(gè)有界的隊(duì)列。那么常見的代碼可以是這樣:
static?class?Buf?{
????private?final?int?MAX?=?5;
????private?final?ArrayList?list?=?new?ArrayList<>();
????synchronized?void?put(int?v)?throws?InterruptedException?{
????????if?(list.size()?==?MAX)?{
????????????wait();
????????}
????????list.add(v);
????????notifyAll();
????}
????synchronized?int?get()?throws?InterruptedException?{
????????//?line?0?
????????if?(list.size()?==?0)?{??//?line?1
????????????wait();??//?line2
????????????//?line?3
????????}
????????int?v?=?list.remove(0);??//?line?4
????????notifyAll();?//?line?5
????????return?v;
????}
????synchronized?int?size()?{
????????return?list.size();
????}
}
注意到這里用的 if,那么我們來看看它會(huì)報(bào)什么錯(cuò)呢?
下面的代碼用了 1 個(gè)線程來 put,10 個(gè)線程來 get:
final?Buf?buf?=?new?Buf();
ExecutorService?es?=?Executors.newFixedThreadPool(11);
for?(int?i?=?0;?i?1;?i++)
es.execute(new?Runnable()?{
????@Override
????public?void?run()?{
????????while?(true?)?{
????????????try?{
????????????????buf.put(1);
????????????????Thread.sleep(20);
????????????}
????????????catch?(InterruptedException?e)?{
????????????????e.printStackTrace();
????????????????break;
????????????}
????????}
????}
});
for?(int?i?=?0;?i?10;?i++)?{
????es.execute(new?Runnable()?{
????????@Override
????????public?void?run()?{
????????????while?(true?)?{
????????????????try?{
????????????????????buf.get();
????????????????????Thread.sleep(10);
????????????????}
????????????????catch?(InterruptedException?e)?{
????????????????????e.printStackTrace();
????????????????????break;
????????????????}
????????????}
????????}
????});
}
es.shutdown();
es.awaitTermination(1,?TimeUnit.DAYS);
這段代碼很快或者說一開始就會(huì)報(bào)錯(cuò):
java.lang.IndexOutOfBoundsException:?Index:?0,?Size:?0
at?java.util.ArrayList.rangeCheck(ArrayList.java:653)?
at?java.util.ArrayList.remove(ArrayList.java:492)?
at?TestWhileWaitBuf.get(TestWhileWait.java:80)atTestWhileWait2.run(TestWhileWait.java:47)?
at?java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)?
at?java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)?
at?java.lang.Thread.run(Thread.java:745)
很明顯,在 remove 的時(shí)候報(bào)錯(cuò)了。那么我們來分析下:
假設(shè)現(xiàn)在有 A,B 兩個(gè)線程來執(zhí)行 get 操作,我們假設(shè)如下的步驟發(fā)生了:
1. A 拿到了鎖 line 0。
2. A 發(fā)現(xiàn) size==0, (line 1),然后進(jìn)入等待,并釋放鎖 (line 2)。
3. 此時(shí) B 拿到了鎖,line0,發(fā)現(xiàn) size==0,(line 1),然后進(jìn)入等待,并釋放鎖 (line 2)。
4. 這個(gè)時(shí)候有個(gè)線程 C 往里面加了個(gè)數(shù)據(jù) 1,那么 notifyAll 所有的等待的線程都被喚醒了。
5. AB 重新獲取鎖,假設(shè)又是 A 拿到了。然后他就走到 line 3,移除了一個(gè)數(shù)據(jù),(line4) 沒有問題。
6. A 移除數(shù)據(jù)后想通知?jiǎng)e人,此時(shí) list 的大小有了變化,于是調(diào)用了 notifyAll (line5),這個(gè)時(shí)候就把 B 給喚醒了,那么 B 接著往下走。
7. 這時(shí)候 B 就出問題了,因?yàn)槠鋵?shí)此時(shí)的競(jìng)態(tài)條件已經(jīng)不滿足了 (size==0)。B 以為還可以刪除就嘗試去刪除,結(jié)果就跑了異常了。
那么 fix 很簡(jiǎn)單,在 get 的時(shí)候加上 while 就好了:
synchronized?int?get()?throws?InterruptedException?{
??????while?(list.size()?==?0)?{
??????????wait();
??????}
??????int?v?=?list.remove(0);
??????notifyAll();
??????return?v;
??}
同樣的,我們可以嘗試修改 put 的線程數(shù)和 get 的線程數(shù)來發(fā)現(xiàn)如果 put 里面不是 while 的話也是不行的。
我們可以用一個(gè)外部周期性任務(wù)來打印當(dāng)前 list 的大小,你會(huì)發(fā)現(xiàn)大小并不是固定的最大5:
final?Buf?buf?=?new?Buf();
ExecutorService?es?=?Executors.newFixedThreadPool(11);
ScheduledExecutorService?printer?=?Executors.newScheduledThreadPool(1);
printer.scheduleAtFixedRate(new?Runnable()?{
????@Override
????public?void?run()?{
????????System.out.println(buf.size());
????}
},?0,?1,?TimeUnit.SECONDS);
for?(int?i?=?0;?i?10;?i++)
es.execute(new?Runnable()?{
????@Override
????public?void?run()?{
????????while?(true?)?{
????????????try?{
????????????????buf.put(1);
????????????????Thread.sleep(200);
????????????}
????????????catch?(InterruptedException?e)?{
?????????????????e.printStackTrace();
????????????????break;
????????????}
????????}
????}
});
for?(int?i?=?0;?i?1;?i++)?{
????es.execute(new?Runnable()?{
????????@Override
????????public?void?run()?{
????????????while?(true?)?{
????????????????try?{
????????????????????buf.get();
????????????????????Thread.sleep(100);
????????????????}
????????????????catch?(InterruptedException?e)?{
????????????????????e.printStackTrace();
????????????????????break;
????????????????}
????????????}
????????}
????});
}
es.shutdown();
es.awaitTermination(1,?TimeUnit.DAYS);
這里我想應(yīng)該說清楚了為啥必須是 while 還是 if 了。
問:什么時(shí)候用 notifyAll 或者 notify?
大多數(shù)人都會(huì)這么告訴你,當(dāng)你想要通知所有人的時(shí)候就用 notifyAll,當(dāng)你只想通知一個(gè)人的時(shí)候就用 notify。但是我們都知道 notify 實(shí)際上我們是沒法決定到底通知誰的(都是從等待集合里面選一個(gè))。那這個(gè)還有什么存在的意義呢?
在上面的例子中,我們用到了 notifyAll,那么下面我們來看下用 notify 是否可以工作呢?
synchronized?void?put(int?v)?throws?InterruptedException?{
???????if?(list.size()?==?MAX)?{
???????????wait();
???????}
???????list.add(v);
???????notify();
???}
???synchronized?int?get()?throws?InterruptedException?{
???????while?(list.size()?==?0)?{
???????????wait();
???????}
???????int?v?=?list.remove(0);
???????notify();
???????return?v;
???}下面的幾點(diǎn)是 jvm 告訴我們的:
任何時(shí)候,被喚醒的來執(zhí)行的線程是不可預(yù)知。比如有 5 個(gè)線程都在一個(gè)對(duì)象上,實(shí)際上我不知道 下一個(gè)哪個(gè)線程會(huì)被執(zhí)行。
synchronized 語義實(shí)現(xiàn)了有且只有一個(gè)線程可以執(zhí)行同步塊里面的代碼。
那么我們假設(shè)下面的場(chǎng)景就會(huì)導(dǎo)致死鎖:
P – 生產(chǎn)者 調(diào)用 put。
C – 消費(fèi)者 調(diào)用 get。
1. P1 放了一個(gè)數(shù)字1。
2. P2 想來放,發(fā)現(xiàn)滿了,在wait里面等了。
3. P3 想來放,發(fā)現(xiàn)滿了,在 wait 里面等了。
4. C1 想來拿,C2,C3 就在 get 里面等著。
5. C1 開始執(zhí)行,獲取1,然后調(diào)用 notify 然后退出。
如果 C1 把 C2 喚醒了,所以P2 (其他的都得等)只能在put方法上等著。(等待獲取synchoronized (this) 這個(gè)monitor)。
C2 檢查 while 循環(huán)發(fā)現(xiàn)此時(shí)隊(duì)列是空的,所以就在 wait 里面等著。
C3 也比 P2 先執(zhí)行,那么發(fā)現(xiàn)也是空的,只能等著了。
6. 這時(shí)候我們發(fā)現(xiàn) P2、C2、C3 都在等著鎖,最終 P2 拿到了鎖,放一個(gè) 1,notify,然后退出。
7. P2 這個(gè)時(shí)候喚醒了P3,P3發(fā)現(xiàn)隊(duì)列是滿的,沒辦法,只能等它變?yōu)榭铡?br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;">8. 這時(shí)候沒有別的調(diào)用了,那么現(xiàn)在這三個(gè)線程(P3, C2,C3)就全部變成 suspend 了,也就是死鎖了。
來源:程序員小樂
版權(quán)申明:內(nèi)容來源網(wǎng)絡(luò),版權(quán)歸原創(chuàng)者所有。除非無法確認(rèn),我們都會(huì)標(biāo)明作者及出處,如有侵權(quán)煩請(qǐng)告知,我們會(huì)立即刪除并表示歉意。謝謝!

