线程join
class ThreadB extends Thread
{
private String ID="0";
private int time=0;
public ThreadB(String ID,int time)
{
this.ID=ID;
this.time=time;
}
public void run()
{
System.out.println("线程A"+ID+"开始了");
int i=1;
try
{
while(i<=time)
{
System.out.println("线程A"+ID+"运行的 次数"+ i++);
Thread.sleep(200);
}
} catch (Exception e)
{
// TODO: handle exception
e.printStackTrace();
}
System.out.println("线程A"+ID+"结束");
}
}
public class JoinThrea
{
public static void main(String []args)
{
Thread thread1=new ThreadB("1",3);
Thread thread2=new ThreadB("2",10);
Thread thread3=new ThreadB("3",5);
Thread thread4=new ThreadB("4",15);
thread1.start();
try
{
thread1.join();//只有当此线程结束 后面的线程才会开始
} catch (Exception e)
{
// TODO: handle exception
}
thread2.start();
thread3.start();
thread4.start();
try
{
thread1.join();
thread2.join();
thread3.join();
thread4.join();
} catch (InterruptedException e)
{
// TODO: handle exception
e.printStackTrace();
}
System.out.println("全部结束");//只有当以上所有线程结束后才会 运行
}
}
梦里不知身是客,一晌贪欢。