Java 并发 - 如何确保三个线程顺序执行?
1. 使用join
public class ThreadTest1 {
// T1、T2、T3三个线程顺序执行
public static void main(String[] args) {
Thread t1 = new Thread(new Work(null));
Thread t2 = new Thread(new Work(t1));
Thread t3 = new Thread(new Work(t2));
t1.start();
t2.start();
t3.start();
}
static class Work implements Runnable {
private Thread beforeThread;
public Work(Thread beforeThread) {
this.beforeThread = beforeThread;
}
public void run() {
if (beforeThread != null) {
try {
beforeThread.join();
System.out.println("thread start:" + Thread.currentThread().getName());
} catch (InterruptedException e) {
e.printStackTrace();
}
} else {
System.out.println("thread start:" + Thread.currentThread().getName());
}
}
}
}2. 使用CountDownLatch
3. CachedThreadPool
4. 使用blockingQueue
5. 使用单个线程池
最后更新于