在java语言中,join()方法的作用是让调用该方法的线程在执行完run()方法后,再执行join 方法后面的代码。
简单点说就是,将两个线程合并,用于实现同步的功能。
具体而言:可以通过线程A的join() 方法来等待线程A的结束,或者使用线程A的join(1000)方法来等到线程A的结束,但是最多只等待1s.(时间数自己随便改。)
请看如下具体的例子,相信就会明白了
1.最原始的线程
1 public classTestJoin {2
3 public static voidmain(String[] args) {4 Thread t = new Thread(newThreadImp());5 t.start();6 try{7 //主线程的执行
8 for(int i=0;i<=5;i++){9 System.out.println("当前为主线程,此时i为"+i);10 Thread.sleep(1000);11 }12
13 System.out.println("join Finished");14 } catch(InterruptedException e) {15 //TODO Auto-generated catch block
16 e.printStackTrace();17 }18
19 }20
21 }22
23 class ThreadImp implementsRunnable{24
25 @Override26 public voidrun() {27 try{28 System.out.println("ThreadImp is begining");29 Thread.sleep(5000);30
31 System.out.println("ThreadImp End");32
33 }catch(InterruptedException e){34 e.printStackTrace();35 }36
37 }38
39 }
执行结果为:
当前为主线程,此时i为0
ThreadImp is begining
当前为主线程,此时i为1
当前为主线程,此时i为2
当前为主线程,此时i为3
当前为主线程,此时i为4
ThreadImp End
当前为主线程,此时i为5
join Finished
看以看到,最基本的线程之间来回切换执行。
2.当在主线程中代用 t线程的join方法后(不设置等待时间)
public classTestJoin {public static voidmain(String[] args) {
Thread t= new Thread(newThreadImp());
t.start();try{
t.join();//判断t线程是否还在
if(t.isAlive()){
System.out.println("the t has not finished.");
}else{
System.out.println("the t has finished.");
}//主线程的执行
for(int i=0;i<=5;i++){
System.out.println("当前为主线程,此时i为"+i);
Thread.sleep(1000);
}
System.out.println("join Finished");
}catch(InterruptedException e) {//TODO Auto-generated catch block
e.printStackTrace();
}
}
}class ThreadImp implementsRunnable{
@Overridepublic voidrun() {try{
System.out.println("ThreadImp is begining");
Thread.sleep(5000);
System.out.println("ThreadImp End");
}catch(InterruptedException e){
e.printStackTrace();
}
}
}
运行结果为:
ThreadImp is begining
ThreadImp End
the t has finished.
当前为主线程,此时i为0
当前为主线程,此时i为1
当前为主线程,此时i为2
当前为主线程,此时i为3
当前为主线程,此时i为4
当前为主线程,此时i为5
join Finished
可以看出 ,在主线程中调用t.join()方法, 导致t线程结束后再执行后面的主线程。
3. 设置join()等待的时间后
1 public classTestJoin {2
3 public static voidmain(String[] args) {4 Thread t = new Thread(newThreadImp());5 t.start();6 try{7 t.join(2000);//主线程只等待t线程结束,只等待2秒8 //判断t线程是否还在
9 if(t.isAlive()){10 System.out.println("the t has not finished.");11 }else{12 System.out.println("the t has finished.");13 }14 //主线程的执行
15 for(int i=0;i<=5;i++){16 System.out.println("当前为主线程,此时i为"+i);17 Thread.sleep(1000);18 }19
20 System.out.println("join Finished");21 } catch(InterruptedException e) {22 //TODO Auto-generated catch block
23 e.printStackTrace();24 }25
26 }27
28 }29
30 class ThreadImp implementsRunnable{31
32 @Override33 public voidrun() {34 try{35 System.out.println("ThreadImp is begining");36 Thread.sleep(5000);37
38 System.out.println("ThreadImp End");39
40 }catch(InterruptedException e){41 e.printStackTrace();42 }43
44 }45
46 }
运行结果:
ThreadImp is begining
the t has not finished.
当前为主线程,此时i为0
当前为主线程,此时i为1
当前为主线程,此时i为2
当前为主线程,此时i为3
ThreadImp End
当前为主线程,此时i为4
当前为主线程,此时i为5
join Finished
可以得到,t线程运行,等待t线程2s时间后,主线程便迫不及待的执行,此时t线程还没结束,然后t线程和主线程交替执行。