您的位置:首页 > 编程语言 > Java开发

java thread.join()用法研究

2011-04-25 10:34 253 查看
提示:thread.join()应该是让当前线程block住,等thread执行完之后,再继续执行 。 比如有3个线程在执行计算任务,必须等三个线程都执行完才能汇总,那么这时候在主线程里面让三个线程join,最后计算结果既可。

一、在研究join的用法之前,先明确两件事情。

1.join方法定义在Thread类中,则调用者必须是一个线程,

例如:

Thread t = new CustomThread();//这里一般是自定义的线程类

t.start();//线程起动

t.join();//此处会抛出InterruptedException异常

2.上面的两行代码也是在一个线程里面执行的。

以上出现了两个线程,一个是我们自定义的线程类,我们实现了run方法,做一些我们需要的工作;另外一个线程,生成我们自定义线程类的对象,然后执行

customThread.start();

customThread.join();

在这种情况下,两个线程的关系是一个线程由另外一个线程生成并起动,所以我们暂且认为第一个线程叫做“子线程”,另外一个线程叫做“主线程”。

二、为什么要用join()方法

主线程生成并起动了子线程,而子线程里要进行大量的耗时的运算(这里可以借鉴下线程的作用),当主线程处理完其他的事务后,需要用到子线程的处理结果,这个时候就要用到join();方法了。

三、join方法的作用

在网上看到有人说“将两个线程合并”。这样解释我觉得理解起来还更麻烦。不如就借鉴下API里的说法:

“等待该线程终止。”

解释一下,是主线程(我在“一”里已经命名过了)等待子线程的终止。也就是在子线程调用了join()方法后面的代码,只有等到子线程结束了才能执行。(Waits for this thread to die.)

四、用实例来理解

写一个简单的例子来看一下join()的用法,一共三个类:

1.CustomThread 类

2. CustomThread1类

3. JoinTestDemo 类,main方法所在的类。

package com;

/**

*

* @author mars914

*

*/

class CustomThread1 extends Thread {

public CustomThread1() {

super("[CustomThread1] Thread");

};

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 (Exception e) {

System.out.println("Exception from " + threadName + ".run");

}

}

}

class CustomThread extends Thread {

CustomThread1 t1;

public CustomThread(CustomThread1 t1) {

super("[CustomThread] Thread");

this.t1 = t1;

}

public void run() {

String threadName = Thread.currentThread().getName();

System.out.println(threadName + " start.");

try {

t1.join(); //在代碼2里,將此處注釋掉

System.out.println(threadName + " end.");

} catch (Exception e) {

System.out.println("Exception from " + threadName + ".run");

}

}

}

public class JoinTestDemo {

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);

t.start();

t.join();//在代碼2里,將此處注釋掉

} catch (Exception e) {

System.out.println("Exception from main");

}

System.out.println(threadName + " end!");

}

}

执行结果:

main start.
[CustomThread1] Thread start.
[CustomThread1] Threadloop at 0
[CustomThread1] Threadloop at 1
[CustomThread] Thread start.
[CustomThread1] Threadloop at 2
[CustomThread1] Threadloop at 3
[CustomThread1] Threadloop at 4
[CustomThread1] Thread end.
[CustomThread] Thread end.
main end

将上面代码中的35、52行注释掉

执行结果:

main start.
[CustomThread1] Thread start.
[CustomThread1] Threadloop at 0
[CustomThread1] Threadloop at 1
[CustomThread1] Threadloop at 2
main end
[CustomThread] Thread start.
[CustomThread] Thread end.
[CustomThread1] Threadloop at 3
[CustomThread1] Threadloop at 4
[CustomThread1] Thread end.

另外:

如果线程被生成了,但还未被起动,调用它的join()方法是没有作用的。将直接继续向下执行,这里就不写代码验证了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: