Java多线程之join
1.join方法只有在继承了Thread类的线程中才有。
2.线程必须要start()后再join才能起作用。
JDK的解释:join public final void join(long millis)throws InterruptedException Waits at most millis milliseconds for this thread to die. A timeout of 0 means to wait forever.
字面意思是等待一段时间直到这个线程死亡
为什么要用join()方法
join的用法, 在某些情况下,如果子线程里要进行大量的耗时的运算,主线程可能会在子线程执行完之前结束,但是如果主线程又需要用到子线程的处理结果,也就是主线程需要等待子线程执行完成之后再结束,这个时候就要用到join()。
使用实例:
写一个简单的例子来看一下join()的用法,一共三个类:
1.CustomThread 类
2. CustomThread1类
3. JoinTestDemo 类,main方法所在的类。
代码1:
package wxhx.csdn2; 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(); 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.//main方法所在的线程起动,但没有马上结束,因为调用t.join();,所以要等到t结束了,此线程才能向下执行。
[CustomThread1] Thread start.//线程CustomThread1起动
[CustomThread1] Thread loop at 0//线程CustomThread1执行
[CustomThread1] Thread loop at 1//线程CustomThread1执行
[CustomThread] Thread start.//线程CustomThread起动,但没有马上结束,因为调用t1.join();,所以要等到t1结束了,此线程才能向下执行。
[CustomThread1] Thread loop at 2//线程CustomThread1继续执行
[CustomThread1] Thread loop at 3//线程CustomThread1继续执行
[CustomThread1] Thread loop at 4//线程CustomThread1继续执行
[CustomThread1] Thread end. //线程CustomThread1结束了
[CustomThread] Thread end.// 线程CustomThread在t1.join();阻塞处起动,向下继续执行的结果
main end!//线程CustomThread结束,此线程在t.join();阻塞处起动,向下继续执行的结果。
代码2:
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. // main方法所在的线程起动,但没有马上结束,这里并不是因为join方法,而是因为Thread.sleep(2000);
[CustomThread1] Thread start. //线程CustomThread1起动
[CustomThread1] Thread loop at 0//线程CustomThread1执行
[CustomThread1] Thread loop at 1//线程CustomThread1执行
main end!// Thread.sleep(2000);结束,虽然在线程CustomThread执行了t1.join();,但这并不会影响到其他线程(这里main方法所在的线程)。
[CustomThread] Thread start. //线程CustomThread起动,但没有马上结束,因为调用t1.join();,所以要等到t1结束了,此线程才能向下执行。
[CustomThread1] Thread loop at 2//线程CustomThread1继续执行
[CustomThread1] Thread loop at 3//线程CustomThread1继续执行
[CustomThread1] Thread loop at 4//线程CustomThread1继续执行
[CustomThread1] Thread end. //线程CustomThread1结束了
[CustomThread] Thread end. // 线程CustomThread在t1.join();阻塞处起动,向下继续执行的结果