1)现在有T1、T2、T3三个线程,你怎样保证T2在T1执行完后执行,T3在T2执行完后执行?直接上代码(所有程序均以简单明了为主)
原文引用:这个线程问题通常会在第一轮或电话面试阶段被问到,目的是检测你对”join”方法是否熟悉。这个多线程问题比较简单,可以用join方法实现。
自己总结:
//还不如顺序写程序了,不用线程了,哈哈
public class JoinTest
{
public static void main(String[] args) throws InterruptedException
{
Thread1 T1=new Thread1();
Thread2 T2=new Thread2();
Thread3 T3=new Thread3();
T1.start();
T1.join();
T2.start();
T2.join();
T3.start();
T3.join();
}
}
class Thread1 extends Thread
{
public void run()
{
System.out.println("T1 running");
}
}
class Thread2 extends Thread
{
public void run()
{
System.out.println("T2 running");
}
}
class Thread3 extends Thread
{
public void run()
{
System.out.println("T3 running");
}
}
join()表示无限期等待直到该线程结束,join(millis)表示只等待该线程millis毫秒的时间,关于join()具体的介绍大家可参考这篇文章,介绍的比较好 http://www.blogjava.net/vincent/archive/2008/08/23/223912.html
public class JoinTest
{
public static void main(String[] args) throws InterruptedException
{
Thread1 T1=new Thread1();
Thread2 T2=new Thread2();
Thread3 T3=new Thread3();
T1.start();
T1.join();
T2.start();
T2.join();
T3.start();
T3.join();
}
}
class Thread1 extends Thread
{
public void run()
{
System.out.println("T1 running");
}
}
class Thread2 extends Thread
{
public void run()
{
System.out.println("T2 running");
}
}
class Thread3 extends Thread
{
public void run()
{
System.out.println("T3 running");
}
}