一、使用方式。
join是Thread类的一个方法,启动线程后直接调用,例如:
1 Thread t = new AThread(); t.start(); t.join();
二、为什么要用join()方法
在很多情况下,主线程生成并起动了子线程,如果子线程里要进行大量的耗时的运算,主线程往往将于子线程之前结束,但是如果主线程处理完其他的事务后,需要用到子线程的处理结果,也就是主线程需要等待子线程执行完成之后再结束,这个时候就要用到join()方法了。
三、join方法的作用
在JDk的API里对于join()方法是:
joinpublic final void join() throws InterruptedException Waits for this thread to die. Throws: InterruptedException - if any thread has interrupted the current thread. The interrupted status of the current thread is cleared when this exception is thrown.
即join()的作用是:“等待该线程终止”,这里需要理解的就是该线程是指的主线程等待子线程的终止。也就是在子线程调用了join()方法后面的代码,只有等到子线程结束了才能执行。
四、用实例来理解
写一个简单的例子来看一下join()的用法:
1.AThread 类
-
BThread类
TestDemo 类
1 class BThread extends Thread { 2 public BThread() { 3 super("[BThread] Thread"); 4 }; 5 public void run() { 6 String threadName = Thread.currentThread().getName(); 7 System.out.println(threadName + " start."); 8 try { 9 for (int i = 0; i < 5; i++) { 10 System.out.println(threadName + " loop at " + i); 11 Thread.sleep(1000); 12 } 13 System.out.println(threadName + " end."); 14 } catch (Exception e) { 15 System.out.println("Exception from " + threadName + ".run"); 16 } 17 } 18 } 19 class AThread extends Thread { 20 BThread bt; 21 public AThread(BThread bt) { 22 super("[AThread] Thread"); 23 this.bt = bt; 24 } 25 public void run() { 26 String threadName = Thread.currentThread().getName(); 27 System.out.println(threadName + " start."); 28 try { 29 bt.join(); 30 System.out.println(threadName + " end."); 31 } catch (Exception e) { 32 System.out.println("Exception from " + threadName + ".run"); 33 } 34 } 35 } 36 public class TestDemo { 37 public static void main(String[] args) { 38 String threadName = Thread.currentThread().getName(); 39 System.out.println(threadName + " start."); 40 BThread bt = new BThread(); 41 AThread at = new AThread(bt); 42 try { 43 bt.start(); 44 Thread.sleep(2000); 45 at.start(); 46 at.join(); 47 } catch (Exception e) { 48 System.out.println("Exception from main"); 49 } 50 System.out.println(threadName + " end!"); 51 } 52 }
打印结果:
1 main start. //主线程起动,因为调用了at.join(),要等到at结束了,此线程才能向下执行。 2 [BThread] Thread start. 3 [BThread] Thread loop at 0 4 [BThread] Thread loop at 1 5 [AThread] Thread start. //线程at启动,因为调用bt.join(),等到bt结束了才向下执行。 6 [BThread] Thread loop at 2 7 [BThread] Thread loop at 3 8 [BThread] Thread loop at 4 9 [BThread] Thread end. 10 [AThread] Thread end. // 线程AThread在bt.join();阻塞处起动,向下继续执行的结果 11 main end! //线程AThread结束,此线程在at.join();阻塞处起动,向下继续执行的结果。
修改一下代码:
1 public class TestDemo { 2 public static void main(String[] args) { 3 String threadName = Thread.currentThread().getName(); 4 System.out.println(threadName + " start."); 5 BThread bt = new BThread(); 6 AThread at = new AThread(bt); 7 try { 8 bt.start(); 9 Thread.sleep(2000); 10 at.start(); 11 //at.join(); //在此处注释掉对join()的调用 12 } catch (Exception e) { 13 System.out.println("Exception from main"); 14 } 15 System.out.println(threadName + " end!"); 16 } 17 }
打印结果:
1 main start. // 主线程起动,因为Thread.sleep(2000),主线程没有马上结束; 2 3 [BThread] Thread start. //线程BThread起动 4 [BThread] Thread loop at 0 5 [BThread] Thread loop at 1 6 main end! // 在sleep两秒后主线程结束,AThread执行的bt.join();并不会影响到主线程。 7 [AThread] Thread start. //线程at起动,因为调用了bt.join(),等到bt结束了,此线程才向下执行。 8 [BThread] Thread loop at 2 9 [BThread] Thread loop at 3 10 [BThread] Thread loop at 4 11 [BThread] Thread end. //线程BThread结束了 12 [AThread] Thread end. // 线程AThread在bt.join();阻塞处起动,向下继续执行的结果
五、从源码看join()方法在AThread的run方法里,执行了bt.join();,进入看一下它的JDK源码:
1 public final void join() throws InterruptedException { 2 join(0L); 3 }
然后进入join(0L)方法:
1 public final synchronized void join(long l) 2 throws InterruptedException 3 { 4 long l1 = System.currentTimeMillis(); 5 long l2 = 0L; 6 if(l < 0L) 7 throw new IllegalArgumentException("timeout value is negative"); 8 if(l == 0L) 9 for(; isAlive(); wait(0L)); 10 else 11 do 12 { 13 if(!isAlive()) 14 break; 15 long l3 = l - l2; 16 if(l3 <= 0L) 17 break; 18 wait(l3); 19 l2 = System.currentTimeMillis() - l1; 20 } while(true); 21 }
单 纯从代码上看: * 如果线程被生成了,但还未被起动,isAlive()将返回false,调用它的join()方法是没有作用的。将直接继续向下执行。 * 在AThread类中的run方法中,bt.join()是判断bt的active状态,如果bt的isActive()方法返回false,在 bt.join(),这一点就不用阻塞了,可以继续向下进行了。从源码里看,wait方法中有参数,也就是不用唤醒谁,只是不再执行wait,向下继续执 行而已。 * 在join()方法中,对于isAlive()和wait()方法的作用对象是个比较让人困惑的问题:
isAlive()方法的签名是:public final native boolean isAlive(),也就是说isAlive()是判断当前线程的状态,也就是bt的状态。
wait()方法在jdk文档中的解释如下:
Causes the current thread to wait until another thread invokes the notify() method or the notifyAll() method for this object. In other words, this method behaves exactly as if it simply performs the call wait(0).
The current thread must own this object's monitor. The thread releases ownership of this monitor and waits until another thread notifies threads waiting on this object's monitor to wake up either through a call to the notify method or the notifyAll method. The thread then waits until it can re-obtain ownership of the monitor and resumes execution.
在这里,当前线程指的是at。