多线程 之 join

来源:互联网 发布:如何下载excel软件 编辑:程序博客网 时间:2024/06/16 22:39

join 等待该线程终止.

抛出: InterrupterException -如果任何线程中断当前线程。当前线程的中断状态被清除.


class Demo implements Runnable{    boolean flag=false;    public synchronized void run() {        if(flag) try {wait();} catch (Exception e){}        for(int i=0;i<100;i++)        System.out.println(Thread.currentThread().getName()+"..."+i);    }}public class Main {    public static void main(String[] args) throws Exception    {        Demo d1=new Demo();        Demo d2=new Demo();        Demo d3=new Demo();        Thread t1=new Thread(d1);        Thread t2=new Thread(d2);        Thread t3=new Thread(d3);        //运行到join的时候 必须要让这个线程t1 执行完 才运行下面的线程        //如果 t1 wait()了 就会一直等待        //d1.flag=true;        t1.start();        t1.join();        t2.start();        t3.start();    }}


原创粉丝点击