面试遇到这样一个问题:如果你有三个线程,分别为T1,T2,T3,如何让线程T2在线程T1之后执行,在线程T3之前执行。
public class Test {
public static void main(String[] args) throws InterruptedException {
final Thread t1 = new Thread(new Runnable(){
@Override
public void run() {
try {
Thread.sleep(5000);
System.out.println(Thread.currentThread().getName());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
},"Thread-T1");
final Thread t2 = new Thread(new Runnable(){
@Override
public void run() {
try {
t1.join();
Thread.sleep(3000);
System.out.println(Thread.currentThread().getName());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
},"Thread-T2");
final Thread t3 = new Thread(new Runnable(){
@Override
public void run() {
try {
t2.join();
Thread.sleep(1000);
System.out.println(Thread.currentThread().getName());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
},"Thread-T3");
t3.start();
t2.start();
t1.start();
}
}
thread.Join把指定的线程加入到当前线程,可以将两个交替执行的线程合并为顺序执行的线程。比如在线程B中调用了线程A的Join()方法,直到线程A执行完毕后,才会继续执行线程B。
t.join(); //使调用线程 t 在此之前执行完毕。
t.join(1000); //等待 t 线程,等待时间是1000毫秒
t.join(1000); //等待 t 线程,等待时间是1000毫秒