thread.Join把指定的线程加入到当前线程,可以将两个交替执行的线程合并为顺序执行的线程。比如在线程B中调用了线程A的Join()方法,直到线程A执行完毕后,才会继续执行线程B。
t.join(); //使调用线程 t 在此之前执行完毕。
t.join(1000); //等待 t 线程,等待时间是1000毫秒
先上一段JDK中代码:
public final synchronized void join(longmillis)throwsInterruptedException {long base =System.currentTimeMillis();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= System.currentTimeMillis() -base;
}
}
}
从代码上看,如果线程被生成了,但还未被起动,调用它的
join()
方法是没有作用的,将直接继续向下执行
Join方法实现是通过wait(小提示:Object 提供的方法)。 当main线程调用t.join时候,main线程会获得线程对象t的锁(wait 意味着拿到该对象的锁),调用该对象的wait(等待时间),直到该对象唤醒main线程
,比如退出后。这就意味着main
线程调用t.join时,必须能够拿到线程t对象的锁
public classJoinTest {public static void main(String[] arg) throwsInterruptedException {
Thread1 t1=newThread1();
Thread2 t2=newThread2();
t1.start();
t2.start();
t1.join();//t2.start();
System.out.println("主线程");
}public static class Thread1 extendsThread{
@Overridepublic voidrun() {try{
Thread.sleep(2000);
}catch(Exception e) {
}
sayHello(this);
}
}public static class Thread2 extendsThread{
@Overridepublic voidrun() {try{
Thread.sleep(500);
}catch(InterruptedException e) {
}
sayHello(this);
}
}public static voidsayHello(Object o){
System.out.println("公用方法"+o.getClass().getName());
}
}
说明:从源码中可以看出,join(),其实是调用了wait()方法,wait方法是不会阻塞线程的,而sleep是会阻塞线程。当主线程等待的时候,并没有影响t2的运行,所以t2正常打印。
wait()和sleep()的区别可以用这样一个图表示