赞
踩
例子1:join使用
class MyThread07 extends Thread { @Override public void run() { try { long time = (long) (Math.random() * 1000); System.out.println(time); Thread.sleep(time); } catch (InterruptedException e) { e.printStackTrace(); } } } public class StudyThreads07join方法的使用 { public static void main(String[] args) throws InterruptedException { MyThread07 myThread07 = new MyThread07(); myThread07.start(); myThread07.join(); System.out.println("我想在myThread07执行完毕后执行。。。"); System.out.println("join 方法可以实现。。。"); } }
例2:
class MyThread07 extends Thread { @Override public void run() { try { long time = (long) (Math.random() * 1000); System.out.println(time); Thread.sleep(2000); System.out.println("线程执行完毕。。。"); } catch (InterruptedException e) { e.printStackTrace(); } } } public class StudyThreads07join方法的使用 { public static void main(String[] args) throws InterruptedException { MyThread07 myThread07 = new MyThread07(); myThread07.start(); // 规定时间内 myThread07 没有执行完毕也会继续向下执行,类似超时 // 如果myThread07比规定时间提前结束,主线程直接继续向下执行, myThread07.join(10000); System.out.println("我想在myThread07执行完毕后执行。。。"); System.out.println("join 方法可以实现。。。"); } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。