點擊上方“朱小廝的博客”,選擇“設爲星標”
後臺回覆"加羣",加入新技術
來源:8rr.co/6cj5
問:爲什麼是 while 而不是 if ?
大多數人都知道常見的使用 synchronized 代碼:
synchronized (obj) {
while (check pass) {
wait();
}
// do your business
}
那麼問題是爲啥這裏是 while 而不是 if 呢?這個問題我最開始也想了很久,按理來說已經在 synchronized 塊裏面了嘛,就不需要了。這個也是我前面一直是這麼認爲的,直到最近看了一個 Stackoverflow 上的問題纔對這個問題有了比較深入的理解。
試想我們要試想一個有界的隊列。那麼常見的代碼可以是這樣:
static class Buf {
private final int MAX = 5;
private final ArrayList<Integer> 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 的時候報錯了。那麼我們來分析下:
假設現在有 A,B 兩個線程來執行 get 操作,我們假設如下的步驟發生了:
1. A 拿到了鎖 line 0。
2. A 發現 size==0, (line 1),然後進入等待,並釋放鎖 (line 2)。
3. 此時 B 拿到了鎖,line0,發現 size==0,(line 1),然後進入等待,並釋放鎖 (line 2)。
4. 這個時候有個線程 C 往裏面加了個數據 1,那麼 notifyAll 所有的等待的線程都被喚醒了。
5. AB 重新獲取鎖,假設又是 A 拿到了。然後他就走到 line 3,移除了一個數據,(line4) 沒有問題。
6. A 移除數據後想通知別人,此時 list 的大小有了變化,於是調用了 notifyAll (line5),這個時候就把 B 給喚醒了,那麼 B 接着往下走。
7. 這時候 B 就出問題了,因爲其實此時的競態條件已經不滿足了 (size==0)。B 以爲還可以刪除就嘗試去刪除,結果就跑了異常了。
那麼 fix 很簡單,在 get 的時候加上 while 就好了:
synchronized int get() throws InterruptedException {
while (list.size() == 0) {
wait();
}
int v = list.remove(0);
notifyAll();
return v;
}
同樣的,我們可以嘗試修改 put 的線程數和 get 的線程數來發現如果 put 裏面不是 while 的話也是不行的。
我們可以用一個外部週期性任務來打印當前 list 的大小,你會發現大小並不是固定的最大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);
這裏我想應該說清楚了爲啥必須是 while 還是 if 了。
問:什麼時候用 notifyAll 或者 notify?
大多數人都會這麼告訴你,當你想要通知所有人的時候就用 notifyAll,當你只想通知一個人的時候就用 notify。但是我們都知道 notify 實際上我們是沒法決定到底通知誰的(都是從等待集合裏面選一個)。那這個還有什麼存在的意義呢?
在上面的例子中,我們用到了 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;
}
下面的幾點是 jvm 告訴我們的:
任何時候,被喚醒的來執行的線程是不可預知。比如有 5 個線程都在一個對象上,實際上我不知道 下一個哪個線程會被執行。
synchronized 語義實現了有且只有一個線程可以執行同步塊裏面的代碼。
那麼我們假設下面的場景就會導致死鎖:
P – 生產者 調用 put。
C – 消費者 調用 get。
1. P1 放了一個數字1。
2. P2 想來放,發現滿了,在wait裏面等了。
3. P3 想來放,發現滿了,在 wait 裏面等了。
4. C1 想來拿,C2,C3 就在 get 裏面等着。
5. C1 開始執行,獲取1,然後調用 notify 然後退出。
如果 C1 把 C2 喚醒了,所以P2 (其他的都得等)只能在put方法上等着。(等待獲取synchoronized (this) 這個monitor)。
C2 檢查 while 循環發現此時隊列是空的,所以就在 wait 裏面等着。
C3 也比 P2 先執行,那麼發現也是空的,只能等着了。
6. 這時候我們發現 P2、C2、C3 都在等着鎖,最終 P2 拿到了鎖,放一個 1,notify,然後退出。
7. P2 這個時候喚醒了P3,P3發現隊列是滿的,沒辦法,只能等它變爲空。
8. 這時候沒有別的調用了,那麼現在這三個線程(P3, C2,C3)就全部變成 suspend 了,也就是死鎖了。
想知道更多?掃描下面的二維碼關注我後臺回覆”加羣“獲取公衆號專屬羣聊入口
【原創系列 | 精彩推薦】
Paxos、Raft不是一致性算法嘛?
越說越迷糊的CAP
分佈式事務科普——初識篇
分佈式事務科普——終結篇
面試官居然問我Raft爲什麼會叫做Raft!
面試官給我挖坑:URI中的//有什麼用
面試官給我挖坑:a[i][j]和a[j][i]有什麼區別?
面試官給我挖坑:單機併發TCP連接數到底有多少?
網關Zuul科普
網關Spring Cloud Gateway科普
Nginx架構原理科普
OpenResty概要及原理科普
微服務網關 Kong 科普
雲原生網關Traefik科普
點個在看少個 bug ????