多线程依赖打印(join)

package japan.example.test;

public class ThreadJoinTest {

    public static void main(String[] args) throws InterruptedException {

        new ThreadJoinTest().test();
    }

    public void test() throws InterruptedException {

        Thread[] tt = new Thread[100];
        for (int i = 0; i < tt.length; i++) {
            tt[i] = new Thread(new ThreadJoin(i == 0 ? null : tt[i - 1]));
        }

        for (int i = 0; i < tt.length; i++) {
            tt[i].start();
        }

        Thread.sleep(1000);
    }

}

class ThreadJoin implements Runnable {

    Thread t;

    ThreadJoin(Thread t) {
        this.t = t;
    }

    @Override
    public void run() {
        try {
            if (t != null) {
                t.join();
            }
            String name = Thread.currentThread().getName();
            System.err.println("name|{}" + name);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }
}

 

posted on 2018-02-12 13:43  jis117  阅读(204)  评论(0编辑  收藏  举报

导航