标签:start out art trace sys extends run dex except
等待子线程运行完成后,运行主线程。
1 public class MyThread extends Thread{ 2 3 public void run() { 4 System.out.println(this.getName()+":begin..."); 5 try { 6 Thread.sleep(2000); 7 } catch (InterruptedException e) { 8 // TODO Auto-generated catch block 9 e.printStackTrace(); 10 } 11 System.out.println(this.getName()+":end"); 12 } 13 }
1 public class JoinDemo { 2 public static void main(String[] args) { 3 MyThread mt = new MyThread(); 4 mt.start(); 5 try { 6 mt.join(); 7 } catch (InterruptedException e) { 8 // TODO Auto-generated catch block 9 e.printStackTrace(); 10 } 11 System.out.println(Thread.currentThread().getName()+":end"); 12 } 13 }
执行结果:
如果将join方法注释掉,则执行结果为:
可以看到在有join方法的时候,先等子线程运行完了,然后在开始接着运行main线程。
标签:start out art trace sys extends run dex except
原文地址:https://www.cnblogs.com/hwyblog/p/9429686.html