java join

来源:互联网 发布:新东方网络口语班价格 编辑:程序博客网 时间:2024/06/06 14:56


/** * Created by kodulf on 2017/4/11. */public class TestInterupted {    public static void main(String[] args) throws InterruptedException {        testJoin();    }    private static void testJoin() throws InterruptedException {        TestJoin testJoin1 = new TestJoin();        TestJoin testJoin2 = new TestJoin();        testJoin1.start();        // 插入,等到这个线程执行完了才执行其他的。wait for this thread to die,为什么不用顺序执行呢?因为有一些是需要在子线程中执行的        //testJoin1.join();//此时如果执行,那么这个时候只有主线程和testJoin1的线程,那么主线程会等待,等待t1执行完        testJoin2.start();        testJoin1.join();//此时如果执行,那么此时有三个线程主线程,testJoin1,testJoin2,主线程会等待,testJoin1 testJoin2会轮流执行            }    static class TestJoin extends Thread{        @Override        public void run() {            for (int i = 0; i < 10; i++) {                try {                    Thread.sleep(10);                } catch (InterruptedException e) {                    e.printStackTrace();                }                System.out.println(Thread.currentThread().getName()+" "+i);            }        }    }}



0 0