有線程T1,T2和T3,將如何確保線程T2運行後T1後,T3運行在T2線程後。
public class JoinTest{
public static void main(String[] args) {
Thread t1 = new Thread(new Runnable(){
@Override
public void run(){
System.out.println("t1");
}
});
Thread t2 = new Thread(new Runnable(){
@Override
public void run(){
try{
t1.join();
}catch(InterruptedException e){
e.printStackTrace();
}
System.out.println("t2");
}
});
Thread t3 = new Thread(new Runnable(){
@Override
public void run(){
try{
t2.join();
}catch(InterruptedException e){
e.printStackTrace();
}
System.out.println("t3");
}
});
t1.start();
t2.start();
t3.start();
}
}