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();
}
}