线程中的join方法

来源:互联网 发布:macbook藏文件软件 编辑:程序博客网 时间:2024/05/23 07:26
import java.util.concurrent.TimeUnit;public class Join {    public static void main(String[] args) throws InterruptedException {        Thread previous = Thread.currentThread();        for(int i = 0;i< 10 ;i++){            //每个线程拥有前一个线程的引用,需要等待前一个线程终止,才能从等待中返回            Thread thread = new Thread(new Domino(previous),String.valueOf(i));            thread.start();            previous = thread;        }        try {            TimeUnit.SECONDS.sleep(5);        } catch (InterruptedException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }        System.out.println(Thread.currentThread().getName() + " terminate");    }    static class Domino implements Runnable{        private Thread thread;        public Domino(Thread thread){            this.thread = thread;        }        @Override        public void run() {            try{                thread.join();            }catch(InterruptedException e){            }            System.out.println(Thread.currentThread().getName() + " terminate");        }    }}