concurrent之初识CountDownLatch(计数器),多线程并行处理
程序员文章站
2022-05-04 21:40:12
...
CountDownLatch:
是一个同步工具类,它允许一个或多个线程一直等待,直到其他线程执行完后再执行
详细介绍参考:
https://blog.csdn.net/qq812908087/article/details/81112188
用法:
声明计数器以及计数
CountDownLatch countDownLatch = new CountDownLatch(1);
计数器不为0,则线程处于等待唤醒状态。为0,则线程开始执行任务
countDownLatch.await();
计数器减1
countDownLatch.countDown();
场景1:
一个典型应用场景就是启动一个服务时,主线程需要等待多个组件加载完毕,之后再继续执行。
场景2:
类似于赛跑,将多个线程放到起点,等待发令枪响,然后同时开跑。做法是初始化一个共享的CountDownLatch(1),将其计数器初始化为1,多个线程在开始执行任务前首先 coundownlatch.await(),当主线程调用 countDown() 时,计数器变为0,多个线程同时被唤醒。
测试代码:
package com.demo;
import java.util.concurrent.CountDownLatch;
/**
* @Description
* @Author by mocar小师兄
* @Date 2020/3/12 14:17
**/
public class CountDownLatch_one {
private static final int testCount = Runtime.getRuntime().availableProcessors();
private static class TaskTest implements Runnable{
private CountDownLatch countDownLatch;
private CountDownLatch await;
public TaskTest(CountDownLatch countDownLatch, CountDownLatch await) {
this.countDownLatch = countDownLatch;
this.await = await;
}
@Override
public void run() {
try {
countDownLatch.await();
System.out.println(Thread.currentThread().getName() + "\t 子线程启动组件");
await.countDown();
} catch (InterruptedException e) {
e.printStackTrace();
}finally {
System.out.println(Thread.currentThread().getName() + "\t 子线程启动组件完成");
}
}
}
/**
* 功能描述: 线程并行处理
* 〈〉
* @Param: [args]
* @Return: void
* @Author: by
* @Date: 2020/3/12 14:31
*/
public static void main(String[] args) {
CountDownLatch countDownLatch = new CountDownLatch(1);
CountDownLatch await = new CountDownLatch(testCount);
System.out.println("主线程开始启动服务");
for (int i = 0; i <testCount ; i++) {
new Thread(new TaskTest(countDownLatch,await),"线程" + i).start();
}
try {
System.out.println("子线程开始启动组件");
countDownLatch.countDown();
await.await();
System.out.println("子线程完成启动组件");
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("主线程完成启动服务");
}
}
结果显示:
main主线程先执行,然后是开启任务处于等待状态。当countDownLatch这个计数器为0时,开始唤醒子线程并行执行任务,此时主线程处于等待唤醒状态。当多个子线程任务执行完,再执行主线程任务。