The join method allows one thread wait until the completion of another.
If t1 is a Thread object whose thread(t) is currently excuting,
t1.join()
cause the current thread t to pause execution util t1 terminates.
package thread;
public class CustomThread1 extends Thread {
public CustomThread1(){
super("[CustomThread1] Thread");
}
@Override
public void run() {
String threadName = Thread.currentThread().getName();
System.out.println(threadName +" start");
try {
for(int i=0; i<5; i++){
System.out.println(threadName +" loop at"+i);
Thread.sleep(1000);
}
System.out.println(threadName + " end");
} catch (InterruptedException e) {
System.out.println("Exception from "+threadName+".run");
}
}
}package thread;
public class CustomThread extends Thread {
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);//让Main 睡2秒,使得其他thread有机会执行
t.start();
t.join();//运行结果2将此处注释掉
} catch (InterruptedException e) {
System.out.println("Exception from main");
}
System.out.println(threadName + " end!");
}
CustomThread1 t1;
public CustomThread(CustomThread1 t1){
super("[CustomThread] Thread");
this.t1 = t1;
}
@Override
public void run(){
String threadName = Thread.currentThread().getName();
System.out.println(threadName + " start ");
// If t is a Thread object whose thread is currently executing,
//t.join()causes the current thread to pause execution until t's thread terminates
try {
t1.join();
System.out.println(threadName+" end.");
} catch (InterruptedException e) {
System.out.println("Exception from "+threadName+".run");
}
}
}
1.加入t.join()运行结果
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.注释掉t.join()运行结果
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源码:

2

3

然后进入join(0)方法:

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

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