Thread系列1-浅析 Java Thread.join()

来源:互联网 发布:exm网络意思 编辑:程序博客网 时间:2024/06/02 13:16

声明:这是转载的。

源贴地址:http://blog.csdn.net/bzwm/article/details/3881392


转在这里是为了自己学习的方便,并且做了部分小小的改变。

【正文】

join方法的作用

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

“等待该线程终止。”

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


2

用实例来理解:

写一个简单的例子来看一下join()的用法,一共三个类:
1.CustomThread 类
2. CustomThread1类
3. JoinTestDemo 类,main方法所在的类。


代码:

package wxhx.csdn2;  /**  *   * @author bzwm  *  */  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();阻塞处起动,向下继续执行的结果。


3 项目中实际用到的例子

……Process proc = Runtime.getRuntime().exec(cmd.toString(), null, workFolder);java.io.Writer out = new PrintWriter(System.out);WatchThread stdT = new WatchThread(out, proc.getInputStream(), "STD");WatchThread errT = new WatchThread(out, proc.getErrorStream(), "ERR");stdT.start();errT.start();proc.waitFor();stdT.join();errT.join();……