第一种方法是join;第二种方法是线程池newSingleThreadExecutor
final Thread t1 = new Thread(new Runnable() {
@Override public void run() {
System.out.println("1");
}
});
final Thread t2 = new Thread(new Runnable() {
@Override public void run() {
try {
t1.join();
} catch (InterruptedException e) {
e.printStackTrace(); }
System.out.println("2");
}
});
Thread t3 = new Thread(new Runnable() {
@Override public void run() {
try {
t2.join();
} catch (InterruptedException e) {
e.printStackTrace(); }
System.out.println("3"); } });
t1.start();
t2.start();
t3.start();
final Thread t1 = new Thread(new Runnable() {
@Override public void run() {
System.out.println("1");
}
});
final Thread t2 = new Thread(new Runnable() {
@Override public void run() {
System.out.println("2");
}
});
Thread t3 = new Thread(new Runnable() {
@Override public void run() {
System.out.println("3");
}
});
ExecutorService es = Executors.newSingleThreadExecutor();
es.submit(t1);
es.submit(t2);
es.submit(t3);
es.shutdown();
网友评论