再見面試官:為什么 wait() 方法需要寫在 while 里,而不是 if?
問:為什么是 while 而不是 if ?
大多數(shù)人都知道常見的使用 synchronized 代碼:
synchronized?(obj)?{
?????while?(check?pass)?{
????????wait();
????}
????//?do?your?business
}
那么問題是為啥這里是 while 而不是 if 呢?這個問題我最開始也想了很久,按理來說已經(jīng)在 synchronized 塊里面了嘛,就不需要了。這個也是我前面一直是這么認(rèn)為的,直到最近看了一個 Stackoverflow 上的問題才對這個問題有了比較深入的理解。
試想我們要試想一個有界的隊列。那么常見的代碼可以是這樣:
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,那么我們來看看它會報什么錯呢?下面的代碼用了 1 個線程來 put,10 個線程來 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);
這段代碼很快或者說一開始就會報錯:
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è)現(xiàn)在有 A,B 兩個線程來執(zhí)行 get 操作,我們假設(shè)如下的步驟發(fā)生了:
\1. A 拿到了鎖 line 0。
\2. A 發(fā)現(xiàn) size==0, (line 1),然后進(jìn)入等待,并釋放鎖 (line 2)。
\3. 此時 B 拿到了鎖,line0,發(fā)現(xiàn) size==0,(line 1),然后進(jìn)入等待,并釋放鎖 (line 2)。
\4. 這個時候有個線程 C 往里面加了個數(shù)據(jù) 1,那么 notifyAll 所有的等待的線程都被喚醒了。
\5. AB 重新獲取鎖,假設(shè)又是 A 拿到了。然后他就走到 line 3,移除了一個數(shù)據(jù),(line4) 沒有問題。
\6. A 移除數(shù)據(jù)后想通知別人,此時 list 的大小有了變化,于是調(diào)用了 notifyAll (line5),這個時候就把 B 給喚醒了,那么 B 接著往下走。
\7. 這時候 B 就出問題了,因?yàn)槠鋵?shí)此時的競態(tài)條件已經(jīng)不滿足了 (size==0)。B 以為還可以刪除就嘗試去刪除,結(jié)果就跑了異常了。
那么 fix 很簡單,在 get 的時候加上 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 的話也是不行的。
我們可以用一個外部周期性任務(wù)來打印當(dāng)前 list 的大小,你會發(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 了。
問:什么時候用 notifyAll 或者 notify?
大多數(shù)人都會這么告訴你,當(dāng)你想要通知所有人的時候就用 notifyAll,當(dāng)你只想通知一個人的時候就用 notify。但是我們都知道 notify 實(shí)際上我們是沒法決定到底通知誰的(都是從等待集合里面選一個)。那這個還有什么存在的意義呢?
在上面的例子中,我們用到了 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 告訴我們的:
任何時候,被喚醒的來執(zhí)行的線程是不可預(yù)知。比如有 5 個線程都在一個對象上,實(shí)際上我不知道 下一個哪個線程會被執(zhí)行。 synchronized 語義實(shí)現(xiàn)了有且只有一個線程可以執(zhí)行同步塊里面的代碼。
那么我們假設(shè)下面的場景就會導(dǎo)致死鎖:
P – 生產(chǎn)者 調(diào)用 put。C – 消費(fèi)者 調(diào)用 get。
P1 放了一個數(shù)字1。
P2 想來放,發(fā)現(xiàn)滿了,在wait里面等了。
P3 想來放,發(fā)現(xiàn)滿了,在 wait 里面等了。
C1 想來拿,C2,C3 就在 get 里面等著。
C1 開始執(zhí)行,獲取1,然后調(diào)用 notify 然后退出。
如果 C1 把 C2 喚醒了,所以P2 (其他的都得等)只能在put方法上等著。(等待獲取synchoronized (this) 這個monitor)。 C2 檢查 while 循環(huán)發(fā)現(xiàn)此時隊列是空的,所以就在 wait 里面等著。 C3 也比 P2 先執(zhí)行,那么發(fā)現(xiàn)也是空的,只能等著了。 這時候我們發(fā)現(xiàn) P2、C2、C3 都在等著鎖,最終 P2 拿到了鎖,放一個 1,notify,然后退出。
P2 這個時候喚醒了P3,P3發(fā)現(xiàn)隊列是滿的,沒辦法,只能等它變?yōu)榭铡?/p>
這時候沒有別的調(diào)用了,那么現(xiàn)在這三個線程(P3, C2,C3)就全部變成 suspend 了,也就是死鎖了。
END
免費(fèi)領(lǐng)取 1000+ 道面試資料!!小編這里有一份面試寶典《Java 核心知識點(diǎn).pdf》,覆蓋了 JVM,鎖、高并發(fā)、Spring原理、微服務(wù)、數(shù)據(jù)庫、Zookeep人、數(shù)據(jù)結(jié)構(gòu)等等知識點(diǎn),包含 Java 后端知識點(diǎn) 1000+ 個,部分如下:


