先看一个示例:
子线程A:
class ThreadTesterA implements Runnable { private int counter; @Override public void run() { while (counter <= 5) { System.out.println("ThreadTesterA Counter = " + counter + " "); try { Thread.sleep(50); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } counter++; } System.out.println(); } }
子线程B:
class ThreadTesterB implements Runnable { private int i; @Override public void run() { while (i <= 5) { System.out.println("ThreadTesterB i = " + i + " "); i++; } System.out.println(); } }
主线程:
public class ThreadTester { public static void main(String[] args) throws InterruptedException { Thread t1 = new Thread(new ThreadTesterA()); Thread t2 = new Thread(new ThreadTesterB()); t1.start(); //t1.join(); // wait t1 to be finished t2.start(); //t2.join(); // in this program, this may be removed } }
结果:
ThreadTesterA Counter = 0 ThreadTesterA Counter = 1 ThreadTesterA Counter = 2 ThreadTesterA Counter = 3 ThreadTesterA Counter = 4 ThreadTesterA Counter = 5 ThreadTesterB i = 0 ThreadTesterB i = 1 ThreadTesterB i = 2 ThreadTesterB i = 3 ThreadTesterB i = 4 ThreadTesterB i = 5 //注释了t1.join()和.join()的结果 ThreadTesterA Counter = 0 ThreadTesterB i = 0 ThreadTesterB i = 1 ThreadTesterB i = 2 ThreadTesterB i = 3 ThreadTesterB i = 4 ThreadTesterB i = 5 ThreadTesterA Counter = 1 ThreadTesterA Counter = 2 ThreadTesterA Counter = 3 ThreadTesterA Counter = 4 ThreadTesterA Counter = 5
t1启动后,调用join()方法,直到t1的计数任务结束,才轮到t2启动,然后t2也开始计数任务。可以看到实例中,两个线程就按着严格的顺序来执行了。
如果t2的执行需要依赖于t1中的完整数据的时候,这种方法就可以很好的确保两个线程的同步性。
一、join用法
1.join方法定义在Thread类中,则调用者必须是一个线程
例如:
Thread t = new CustomThread();//这里一般是自定义的线程类 t.start();//线程起动 t.join();//此处会抛出InterruptedException异常
2.上面的两行代码也是在一个线程里面执行的
以上出现了两个线程,一个是我们自定义的线程类,我们实现了run方法,做一些我们需要的工作;另外一个线程,生成我们自定义线程类的对象,然后执行
customThread.start();
customThread.join();
在这种情况下,两个线程的关系是一个线程由另外一个线程生成并起动,所以我们暂且认为第一个线程叫做“子线程”,另外一个线程叫做“主线程”。
二、为什么要用join()方法
主线程生成并启动了子线程,而子线程里要进行大量的耗时的运算(这里可以借鉴下线程的作用),当主线程处理完其他的事务后,需要用到子线程的处理结果,这个时候就要用到join();方法了。
三、join方法的作用
在网上看到有人说“将两个线程合并”。这样解释我觉得理解起来还更麻烦。不如就借鉴下API里的说法:
“等待该线程终止。”
解释一下,是主线程等待子线程的终止。也就是说主线程的代码块中,如果碰到了t.join()方法,此时主线程需要等待,等待子线程结束了(Waits for this thread to die.),才能继续执行t.join()之后的代码块。
四、从源码看join()方法
public final void join() throws InterruptedException { join(0); }
/** * Waits at most <code>millis</code> milliseconds for this thread to * die. A timeout of <code>0</code> means to wait forever. //注意这句 * * @param millis the time to wait in milliseconds. * @exception InterruptedException if another thread has interrupted * the current thread. The <i>interrupted status</i> of the * current thread is cleared when this exception is thrown. */ public final synchronized void join(long millis) //参数millis为0. throws InterruptedException { long base = System.currentTimeMillis(); long now = 0; if (millis < 0) { throw new IllegalArgumentException("timeout value is negative"); } if (millis == 0) {//进入这个分支 while (isAlive()) {//判断本线程是否为活动的。这里的本线程就是t1. wait(0);//阻塞 } } else { while (isAlive()) { long delay = millis - now; if (delay <= 0) { break; } wait(delay); now = System.currentTimeMillis() - base; } } }
原文:http://www.cnblogs.com/duanxz/p/5038471.html