---
title: "浅析 Java Thread.join()"
author: "snowtigersoft"
date: "2009-11-11T10:58:52Z"
tags: ["Blog", "JDK", "Java", "thread", "活动"]
categories: ["编程语言"]
slug: "java-thread-join"
draft: false
lastmod: "2010-03-08T20:17:26Z"
id: 514231
url: "https://snowtigersoft.iteye.com/blog/514231"
---

**一、 在研究 join 的用法之前，先明确 两件 事情 。**

1.j oin 方法定 义 在 Thread 类 中， 则调 用者必 须 是一个 线 程，

例如：

Thread t = new Custom Thread(); // 这里一般是自定义的线程类

t .start();// 线程起动

t.join();// 此处 会 抛出 InterruptedException 异常

 

2\. 上面的两行代码也是在一个线程里面执行的。

 

以上出现了两个线程，一个是我们自定义的线程类，我们实现了 run 方法，做一些我们需要的工作；另外一个线程，生成我们自定义线程类的对象，然后执行

custom Thread .start();

custom Thread .join();

在这种情况下，两个线程的关系是一个线程由另外一个线程生成并起动，所以我们暂且认为第一个线程叫做“**子线程** ”，另外一个线程叫做“**主线程** ”。

 

**二、为什么要用 join() 方法**

主线程生成并起动了子线程，而子线程里要进行大量的耗时的运算 ( 这里可以借鉴下线程的作用 ) ，当主线程处理完其他的事务后，需要用到子线程的处理结果，这个时候就要用到 join(); 方法了。

 

 

**三、 join 方法的作用**

在网上看到有人说“将两个线程合并”。这样解释我觉得理解起来还更麻烦。不如就借鉴下 API 里的说法：

“等待该线程终止。”

解释一下，是主线程 ( 我在“一”里已经命名过了 ) 等待子线程的终止。也就是在子线程调用了 join() 方法后面的代码，只有等到子线程结束了才能执行。(Waits for this thread to die.)

 

 

**四、用实例来理解**

写一个简单的例子来看一下 join() 的用法，一共三个类：

1.CustomThread 类

2\. CustomThread1 类

3\. JoinTestDemo 类， main 方法所在的类。

 

**代码 1 ：**

01. package  wxhx.csdn2;
02. /\**
03. \*
04. * @author bzwm
05. \*
06. \*/
07. class  CustomThread1  extends  Thread {
08. public  CustomThread1() {
09. super ( "\[CustomThread1] Thread" );
10. };
11. public   void  run() {
12. String threadName = Thread.currentThread().getName();
13. System.out.println(threadName + " start." );
14. try  {
15. for  ( int  i =  0 ; i &lt;  5 ; i++) {
16. System.out.println(threadName + " loop at "  + i);
17. Thread.sleep(1000 );
18. }
19. System.out.println(threadName + " end." );
20. } catch  (Exception e) {
21. System.out.println("Exception from "  + threadName +  ".run" );
22. }
23. }
24. }
25. class  CustomThread  extends  Thread {
26. CustomThread1 t1;
27. public  CustomThread(CustomThread1 t1) {
28. super ( "\[CustomThread] Thread" );
29. this .t1 = t1;
30. }
31. public   void  run() {
32. String threadName = Thread.currentThread().getName();
33. System.out.println(threadName + " start." );
34. try  {
35. t1.join();
36. System.out.println(threadName + " end." );
37. } catch  (Exception e) {
38. System.out.println("Exception from "  + threadName +  ".run" );
39. }
40. }
41. }
42. public   class  JoinTestDemo {
43. public   static   void  main(String\[] args) {
44. String threadName = Thread.currentThread().getName();
45. System.out.println(threadName + " start." );
46. CustomThread1 t1 = new  CustomThread1();
47. CustomThread t = new  CustomThread(t1);
48. try  {
49. t1.start();
50. Thread.sleep(2000 );
51. t.start();
52. t.join();//在代碼2里，將此處注釋掉
53. } catch  (Exception e) {
54. System.out.println("Exception from main" );
55. }
56. System.out.println(threadName + " end!" );
57. }
58. }

<!--THE END-->

01. package  wxhx.csdn2;
02. /\**
03. \*
04. * @author bzwm
05. \*
06. \*/
07. class  CustomThread1  extends  Thread {
08. public  CustomThread1() {
09. super ( "\[CustomThread1] Thread" );
10. };
11. public   void  run() {
12. String threadName = Thread.currentThread().getName();
13. System.out.println(threadName + " start." );
14. try  {
15. for  ( int  i =  0 ; i &lt;  5 ; i++) {
16. System.out.println(threadName + " loop at "  + i);
17. Thread.sleep(1000 );
18. }
19. System.out.println(threadName + " end." );
20. } catch  (Exception e) {
21. System.out.println("Exception from "  + threadName +  ".run" );
22. }
23. }
24. }
25. class  CustomThread  extends  Thread {
26. CustomThread1 t1;
27. public  CustomThread(CustomThread1 t1) {
28. super ( "\[CustomThread] Thread" );
29. this .t1 = t1;
30. }
31. public   void  run() {
32. String threadName = Thread.currentThread().getName();
33. System.out.println(threadName + " start." );
34. try  {
35. t1.join();
36. System.out.println(threadName + " end." );
37. } catch  (Exception e) {
38. System.out.println("Exception from "  + threadName +  ".run" );
39. }
40. }
41. }
42. public   class  JoinTestDemo {
43. public   static   void  main(String\[] args) {
44. String threadName = Thread.currentThread().getName();
45. System.out.println(threadName + " start." );
46. CustomThread1 t1 = new  CustomThread1();
47. CustomThread t = new  CustomThread(t1);
48. try  {
49. t1.start();
50. Thread.sleep(2000 );
51. t.start();
52. t.join();//在代碼2里，將此處注釋掉
53. } catch  (Exception e) {
54. System.out.println("Exception from main" );
55. }
56. System.out.println(threadName + " end!" );
57. }
58. }

 

**打印结果：**

 

main start. //main 方法所在的线程起动，但没有马上结束，因为调用 t.join(); ，所以要等到 t 结束了，此线程才能向下执行。

\[CustomThread1] Thread start. // 线程 CustomThread1 起动

\[CustomThread1] Thread loop at 0 // 线程 CustomThread1 执行

\[CustomThread1] Thread loop at 1 // 线程 CustomThread1 执行

\[CustomThread] Thread start. // 线程 CustomThread 起动，但没有马上结束，因为调用 t1.join(); ，所以要等到 t1 结束了，此线程才能向下执行。

\[CustomThread1] Thread loop at 2 // 线程 CustomThread1 继续执行

\[CustomThread1] Thread loop at 3 // 线程 CustomThread1 继续执行

\[CustomThread1] Thread loop at 4 // 线程 CustomThread1 继续执行

\[CustomThread1] Thread end. // 线程 CustomThread1 结束了

\[CustomThread] Thread end. // 线程 CustomThread 在 t1.join(); 阻塞处起动，向下继续执行的结果

main end! // 线程 CustomThread 结束，此线程在 t.join(); 阻塞处起动，向下继续执行的结果。

 

修改一下代码，得到**代码** **2 ：(这里只写出修改的部分)**

1. **public   class  JoinTestDemo {       public   static   void  main(String\[] args) {           String threadName = Thread.currentThread().getName();           System.out.println(threadName + " start." );           CustomThread1 t1 = new  CustomThread1();           CustomThread t = new  CustomThread(t1);           try  {               t1.start();               Thread.sleep(2000 );               t.start();   //          t.join();//在代碼2里，將此處注釋掉            } catch  (Exception e) {               System.out.println("Exception from main" );           }           System.out.println(threadName + " end!" );       }   }**

**public   class  JoinTestDemo {       public   static   void  main(String\[] args) {           String threadName = Thread.currentThread().getName();           System.out.println(threadName + " start." );           CustomThread1 t1 = new  CustomThread1();           CustomThread t = new  CustomThread(t1);           try  {               t1.start();               Thread.sleep(2000 );               t.start();   //          t.join();//在代碼2里，將此處注釋掉            } catch  (Exception e) {               System.out.println("Exception from main" );           }           System.out.println(threadName + " end!" );       }   }**  

 

**打印结果：**

 

main start. // main 方法所在的线程起动，但没有马上结束，这里并不是因为 join 方法，而是因为 Thread.sleep(2000);

\[CustomThread1] Thread start. // 线程 CustomThread1 起动

\[CustomThread1] Thread loop at 0 // 线程 CustomThread1 执行

\[CustomThread1] Thread loop at 1 // 线程 CustomThread1 执行

main end! // Thread.sleep(2000); 结束，虽然在线程 CustomThread 执行了 t1.join(); ，但这并不会影响到其他线程 ( 这里 main 方法所在的线程 ) 。

\[CustomThread] Thread start. // 线程 CustomThread 起动，但没有马上结束，因为调用 t1.join(); ，所以要等到 t1 结束了，此线程才能向下执行。

\[CustomThread1] Thread loop at 2 // 线程 CustomThread1 继续执行

\[CustomThread1] Thread loop at 3 // 线程 CustomThread1 继续执行

\[CustomThread1] Thread loop at 4 // 线程 CustomThread1 继续执行

\[CustomThread1] Thread end. // 线程 CustomThread1 结束了

\[CustomThread] Thread end. // 线程 CustomThread 在 t1.join(); 阻塞处起动，向下继续执行的结果

 

 

**五、从源码看 join() 方法**

 

在 CustomThread 的 run 方法里，执行了 t1.join(); ，进入看一下它的 JDK 源码：

[](http://blog.csdn.net/bzwm/archive/2009/02/12/3881392.aspx)

1. public   final   void  join()  throws  InterruptedException {
2. n(0 );
3. }

<!--THE END-->

1. public   final   void  join()  throws  InterruptedException {
2. n(0 );
3. }

然后进入 join(0) 方法：

 

01. /\**
02. * Waits at most &lt;code&gt;millis&lt;/code&gt; milliseconds for this thread to
03. * die. A timeout of &lt;code&gt;0&lt;/code&gt; means to wait forever. //注意这句
04. \*
05. * @param      millis   the time to wait in milliseconds.
06. * @exception  InterruptedException if another thread has interrupted
07. *             the current thread.  The &lt;i&gt;interrupted status&lt;/i&gt; of the
08. *             current thread is cleared when this exception is thrown.
09. \*/
10. public   final   synchronized   void  join( long  millis)  //参数millis为0.
11. throws  InterruptedException {
12. long  base = System.currentTimeMillis();
13. long  now =  0 ;
14. if  (millis &lt;  0 ) {
15. throw   new  IllegalArgumentException( "timeout value is negative" );
16. }
17. if  (millis ==  0 ) { //进入这个分支
18. while  (isAlive()) { //判断本线程是否为活动的。这里的本线程就是t1.
19. wait(0 ); //阻塞
20. }
21. } else  {
22. while  (isAlive()) {
23. long  delay = millis - now;
24. if  (delay &lt;=  0 ) {
25. break ;
26. }
27. wait(delay);
28. now = System.currentTimeMillis() - base;
29. }
30. }
31. }

<!--THE END-->

01. /\**
02. * Waits at most &lt;code&gt;millis&lt;/code&gt; milliseconds for this thread to
03. * die. A timeout of &lt;code&gt;0&lt;/code&gt; means to wait forever. //注意这句
04. \*
05. * @param      millis   the time to wait in milliseconds.
06. * @exception  InterruptedException if another thread has interrupted
07. *             the current thread.  The &lt;i&gt;interrupted status&lt;/i&gt; of the
08. *             current thread is cleared when this exception is thrown.
09. \*/
10. public   final   synchronized   void  join( long  millis)  //参数millis为0.
11. throws  InterruptedException {
12. long  base = System.currentTimeMillis();
13. long  now =  0 ;
14. if  (millis &lt;  0 ) {
15. throw   new  IllegalArgumentException( "timeout value is negative" );
16. }
17. if  (millis ==  0 ) { //进入这个分支
18. while  (isAlive()) { //判断本线程是否为活动的。这里的本线程就是t1.
19. wait(0 ); //阻塞
20. }
21. } else  {
22. while  (isAlive()) {
23. long  delay = millis - now;
24. if  (delay &lt;=  0 ) {
25. break ;
26. }
27. wait(delay);
28. now = System.currentTimeMillis() - base;
29. }
30. }
31. }

 

 

 

单纯从代码上看，如果线程被生成了，但还未被起动，调用它的 join() 方法是没有作用的。将直接继续向下执行，这里就不写代码验证了。

 

本文来自CSDN博客，转载请标明出处：http://blog.csdn.net/bzwm/archive/2009/02/12/3881392.aspx