java基本教程之join方法的詳解

本章涉及到的內容包括:

java基本教程之join方法的詳解

1. join()介紹

2. join()源碼分析(基於JDK1.7.0_40)

3. join()示例

1. join()介紹

join() 定義在中。

join() 的作用:讓“主線程”等待“子線程”結束之後才能繼續運行。這句話可能有點晦澀,我們還是通過例子去理解:

複製代碼 代碼如下:

// 主線程

public class Father extends Thread {

public void run() {

Son s = new Son();

t();

();

...

}

}

// 子線程

public class Son extends Thread {

public void run() {

...

}

}

說明:

上面的有兩個類Father(主線程類)和Son(子線程類)。因爲Son是在Father中創建並啓動的,所以,Father是主線程類,Son是子線程類。

在Father主線程中,通過new Son()新建“子線程s”。接着通過t()啓動“子線程s”,並且調用()。在調用()之後,Father主線程會一直等待,直到“子線程s”運行完畢;在“子線程s”運行完畢之後,Father主線程才能接着運行。 這也就是我們所說的“join()的作用,是讓主線程會等待子線程結束之後才能繼續運行”!

2. join()源碼分析(基於JDK1.7.0_40)

複製代碼 代碼如下:

public final void join() throws InterruptedException {

join(0);

}

public final synchronized void join(long millis)

throws InterruptedException {

long base = entTimeMillis();

long now = 0;

if (millis < 0) {

throw new IllegalArgumentException("timeout value is negative");

}

if (millis == 0) {

while (isAlive()) {

wait(0);

}

} else {

while (isAlive()) {

long delay = millis - now;

if (delay <= 0) {

break;

}

wait(delay);

now = entTimeMillis() - base;

}

}

}

說明:

從代碼中,我們可以發現。當millis==0時,會進入while(isAlive())循環;即只要子線程是活的`,主線程就不停的等待。

我們根據上面解釋join()作用時的代碼來理解join()的用法!

問題:

雖然()被調用的地方是發生在“Father主線程”中,但是()是通過“子線程s”去調用的join()。那麼,join()方法中的isAlive()應該是判斷“子線程s”是不是Alive狀態;對應的wait(0)也應該是“讓子線程s”等待纔對。但如果是這樣的話,()的作用怎麼可能是“讓主線程等待,直到子線程s完成爲止”呢,應該是讓"子線程等待纔對(因爲調用子線程對象s的wait方法嘛)"?

答案:wait()的作用是讓“當前線程”等待,而這裏的“當前線程”是指當前在CPU上運行的線程。所以,雖然是調用子線程的wait()方法,但是它是通過“主線程”去調用的;所以,休眠的是主線程,而不是“子線程”!

3. join()示例

在理解join()的作用之後,接下來通過示例查看join()的用法。

複製代碼 代碼如下:

// 的源碼

public class JoinTest{

public static void main(String[] args){

try {

ThreadA t1 = new ThreadA("t1"); // 新建“線程t1”

t(); // 啓動“線程t1”

(); // 將“線程t1”加入到“主線程main”中,並且“主線程main()會等待它的完成”

tf("%s finishn", entThread()ame());

} catch (InterruptedException e) {

tStackTrace();

}

}

static class ThreadA extends Thread{

public ThreadA(String name){

super(name);

}

public void run(){

tf("%s startn", ame());

// 延時操作

for(int i=0; i<1000000; i++)

;

tf("%s finishn", ame());

}

}

}

運行結果:

複製代碼 代碼如下:

t1 start

t1 finish

main finish

結果說明:

運行流程如圖

(01) 在“主線程main”中通過 new ThreadA("t1") 新建“線程t1”。 接着,通過 t() 啓動“線程t1”,並執行()。

(02) 執行()之後,“主線程main”會進入“阻塞狀態”等待t1運行結束。“子線程t1”結束之後,會喚醒“主線程main”,“主線程”重新獲取cpu執行權,繼續運行。