JAVA CountDownLatch(倒计时计数器)用法实例
什么是 CountDownLatch
CountDownLatch(倒计时计数器)是 Java 提供的一个同步工具类,通过它可以让一个或多个线程等待其它线程完成各自的工作后再继续执行。
在 CountDownLatch 中,我们可以设置一个计数器的初始值 n,然后调用 countDown()
方法表示这个计数器值减 1,调用 await()
方法表示等待计数器达到 0。
使用 CountDownLatch 可以在主线程等待多个线程执行完毕后再继续执行,或者在多个线程等待某个线程完成任务后再继续执行。
CountDownLatch 示例
示例 1:主线程等待多个线程执行完毕
import java.util.concurrent.CountDownLatch;
public class CountDownLatchExample {
public static void main(String[] args) throws InterruptedException {
int numOfThreads = 5;
CountDownLatch countDownLatch = new CountDownLatch(numOfThreads);
for (int i = 0; i < numOfThreads; i++) {
Thread t = new WorkerThread(countDownLatch, i);
t.start();
}
countDownLatch.await();
System.out.println("All threads have finished executing.");
}
private static class WorkerThread extends Thread {
private final CountDownLatch countDownLatch;
private final int threadId;
public WorkerThread(CountDownLatch countDownLatch, int threadId) {
this.countDownLatch = countDownLatch;
this.threadId = threadId;
}
@Override
public void run() {
try {
System.out.println("Thread " + threadId + " is running.");
Thread.sleep(1000);
System.out.println("Thread " + threadId + " has finished executing.");
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
countDownLatch.countDown();
}
}
}
}
在这个示例中,我们通过 CountDownLatch 让主线程等待 5 个子线程执行完毕后再继续执行。每个子线程运行时休眠 1 秒钟,表示它们正在执行某些操作,然后通过 countDown()
方法将计数器减 1。当计数器减到 0 时,主线程中的 await()
方法将返回,表示所有子线程已经执行完毕。
输出结果如下:
Thread 0 is running.
Thread 1 is running.
Thread 2 is running.
Thread 3 is running.
Thread 4 is running.
Thread 1 has finished executing.
Thread 2 has finished executing.
Thread 0 has finished executing.
Thread 3 has finished executing.
Thread 4 has finished executing.
All threads have finished executing.
示例 2:多个线程等待某个线程完成任务后继续执行
import java.util.concurrent.CountDownLatch;
public class CountDownLatchExample {
public static void main(String[] args) throws InterruptedException {
int numOfThreads = 3;
CountDownLatch countDownLatch = new CountDownLatch(1);
for (int i = 0; i < numOfThreads; i++) {
Thread t = new WaiterThread(countDownLatch, i);
t.start();
}
Thread.sleep(3000);
countDownLatch.countDown();
}
private static class WaiterThread extends Thread {
private final CountDownLatch countDownLatch;
private final int threadId;
public WaiterThread(CountDownLatch countDownLatch, int threadId) {
this.countDownLatch = countDownLatch;
this.threadId = threadId;
}
@Override
public void run() {
try {
System.out.println("Thread " + threadId + " is waiting.");
countDownLatch.await();
System.out.println("Thread " + threadId + " has resumed execution.");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
在这个示例中,我们通过 CountDownLatch 让 3 个子线程等待某个线程完成任务后再继续执行。主线程休眠 3 秒钟,模拟某个需要 3 秒钟才能完成的任务,然后通过 countDown()
方法将计数器减 1。此时由于计数器已经为 0,所有在调用 await()
的子线程都将恢复执行。
输出结果如下:
Thread 1 is waiting.
Thread 0 is waiting.
Thread 2 is waiting.
Thread 0 has resumed execution.
Thread 2 has resumed execution.
Thread 1 has resumed execution.
总结
CountDownLatch 是一个非常实用的同步工具,它可以让我们在多线程编程中更为便捷地实现并发控制。在实际应用中,我们可以根据具体的需求来灵活地使用 CountDownLatch,实现不同的并发控制场景。以上是两个使用 CountDownLatch 的实例,希望能够对大家有所帮助。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:JAVA CountDownLatch(倒计时计数器)用法实例 - Python技术站