淺析Thread.join(),淺析thread.join
概要
本文分為三部分對 Thread.join() 進行分析:
1. join() 的樣本和作用
2. join() 源碼分析
3. 對網上其他分析 join() 的文章提出疑問
1. join() 的樣本和作用1.1 樣本
// 主線程public class Parent extends Thread { public void run() { Child child = new Child(); child .start(); child .join(); // ... }}
// 子線程public class Child extends Thread { public void run() { // ... }}
上面代碼中有兩個類:Parent(主線程類),Child(子線程類)。
在 Parent.run() 中,通過 new Child() 建立 child 子線程(此時 child 處於 NEW 狀態),然後調用 child.start()(child 轉換為 RUNNABLE 狀態),再調用 child.join()。
在 Parent 調用 child.join() 後,child 子線程正常運行,Parant 主線程會等待 child 子線程結束後再繼續運行。
1.2 join() 的作用
讓主線程等待子線程結束之後才能繼續運行。
我們來看看在 Java 7 Concurrency Cookbook 中相關的描述(很清楚地說明了 join() 的作用):
Waiting for the finalization of a thread
In some situations, we will have to wait for the finalization of a thread. For example, we may have a program that will begin initializing the resources it needs before proceeding with the rest of the execution. We can run the initialization tasks as threads and wait for its finalization before continuing with the rest of the program. For this purpose, we can use the join() method of the Thread class. When we call this method using a thread object, it suspends the execution of the calling thread until the object called finishes its execution.
2. join() 源碼分析
以下是 JDK 8 中 join() 的源碼:
1 public final void join() throws InterruptedException { 2 join(0); 3 } 4 5 public final synchronized void join(long millis) 6 throws InterruptedException { 7 long base = System.currentTimeMillis(); 8 long now = 0; 9 10 if (millis < 0) {11 throw new IllegalArgumentException("timeout value is negative");12 }13 14 if (millis == 0) {15 while (isAlive()) {16 wait(0);17 }18 } else {19 while (isAlive()) {20 long delay = millis - now;21 if (delay <= 0) {22 break;23 }24 wait(delay);25 now = System.currentTimeMillis() - base;26 }27 }28 }29 30 public final synchronized void join(long millis, int nanos)31 throws InterruptedException {32 33 if (millis < 0) {34 throw new IllegalArgumentException("timeout value is negative");35 }36 37 if (nanos < 0 || nanos > 999999) {38 throw new IllegalArgumentException(39 "nanosecond timeout value out of range");40 }41 42 if (nanos >= 500000 || (nanos != 0 && millis == 0)) {43 millis++;44 }45 46 join(millis);47 }
我們可以看到 join() 一共有三個版本:
1 public final void join();2 3 public final synchronized void join(long millis);4 5 public final synchronized void join(long millis, int nanos);
其中
a. join() 和 join(long millis, int nanos) 最後都調用了 join(long millis)。
b. 帶參數的 join() 都為 synchronized method。
c. join() 調用了 join(0),從源碼可以看到 join(0) 不斷檢查線程(join() 所屬的線程執行個體,非調用線程)是否是 Active。
以本文開頭的樣本為例我們分析一下代碼的邏輯:
Parent 調用 child.join(),child.join() 再調用 child.join(0) (此時 Parent 會獲得 child 執行個體作為鎖,其他線程可以進入 child.join() ,但不可以進入 child.join(0), 因為沒有獲得鎖),child.join(0) 會不斷地檢查 child 線程是否是 Active。
如果是 Active,則不斷地調用 child.wait(0)(此時 Parent 會釋放 child 執行個體鎖,其他線程可以競爭鎖並進入 child.join(0))。我們可以得知,Parent 線程在不斷地對 child.wait(0) 入棧和出棧。
一旦 child 線程不為 Active (狀態為 TERMINATED), child.join(0) 會直接返回到 child.join(), child.join() 會直接返回到 Parent 主線程,Parent 主線程就可以繼續運行下去了。
3. 對網上其他分析 join() 的文章提出疑問
網上有很多文章的描述我覺得有歧義,下面挑選一些描述進行分析,也歡迎大家留言一起討論。
a. 子線程結束之後,"會喚醒主線程",主線程重新擷取cpu執行權,繼續運行。
“喚醒”令人誤解。其實是主線程調用方法不斷去檢查子線程的狀態, 這是個主動的動作,而不是子線程去喚醒主線程。
b. join() 將幾個並行線程的線程"合并為一個單線程"執行。
我理解提這個說法的人的意思。但是這樣描述只會讓讀者更難理解。
在調用 join() 方法的程式中,原來的多個線程仍然多個線程,並沒有發生“合并為一個單線程”。真正發生的是調用 join() 的線程進入 TIMED_WAITING 狀態,等待 join() 所屬線程運行結束後再繼續運行。
一點感想:技術人員要寫好技術文章,最好不要亂用比喻。沒有能力駕馭中文詞彙,最後只會讓讀者感到更加困惑或形成錯誤的理解。