当前位置:网站首页>JUC-7.0-线程协作-CountDownLatch
JUC-7.0-线程协作-CountDownLatch
2022-07-22 10:58:00 【呵呵呵1112】
述
线程的运行和运行时机是不受我们自己控制的,一些情况下我们可能需要多个线程之间配合来完成一些任务,比如线程A等B线程执行完成之后运行等
CountDownLatch
CountDownLatch
是一个线程协作的工具类,是一个计数器,主要用到的有以下几个方法:
- 构造方法,参数是
count
,是需要倒数的值 await()
: 调用await()
方法的线程会被挂起,等到 count 为 0 的时候才会继续执行countDown()
: 每次调用会将 count 值减1,直到为0的时候等待的线程就会被唤醒
常见用法
用法一
CountDownLatch
的第一种用法就是一个线程等待多个线程都执行完毕,再继续自己的工作,代码示例如下
@Slf4j
public class CountDownLatchTest {
public static void main(String[] args) throws InterruptedException {
ExecutorService threadPool = Executors.newFixedThreadPool(5);
CountDownLatch countDownLatch = new CountDownLatch(5);
for (int i = 0; i < 5; i++) {
threadPool.submit(()->{
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}finally {
log.info("已完成...");
countDownLatch.countDown();
}
});
}
log.info("正在等待其他线程....");
countDownLatch.await();
log.info("所有线程执行完毕...");
}
}
运行输出如下:
10:25:42.359 [main] INFO com.learning.java.cooperation.CountDownLatchTest - 正在等待其他线程....
10:25:43.352 [pool-1-thread-2] INFO com.learning.java.cooperation.CountDownLatchTest - 已完成...
10:25:43.352 [pool-1-thread-3] INFO com.learning.java.cooperation.CountDownLatchTest - 已完成...
10:25:43.352 [pool-1-thread-1] INFO com.learning.java.cooperation.CountDownLatchTest - 已完成...
10:25:43.353 [pool-1-thread-5] INFO com.learning.java.cooperation.CountDownLatchTest - 已完成...
10:25:43.353 [pool-1-thread-4] INFO com.learning.java.cooperation.CountDownLatchTest - 已完成...
10:25:43.353 [main] INFO com.learning.java.cooperation.CountDownLatchTest - 所有线程执行完毕...
首先新建一个 count 为 5 的 CountDownLatch
计数器,然后新建5个线程,之后主线程调用 await()
等待,等待5个线程都调用了 countDown()
之后, count 为 0, 主线程继续运行
用法二
第二种用法是多个线程等待某一个线程的信号,然后同时开始执行,代码示例如下:
@Slf4j
public class CountDownLatchTest {
public static void main(String[] args) throws InterruptedException {
ExecutorService threadPool = Executors.newFixedThreadPool(5);
CountDownLatch countDownLatch = new CountDownLatch(1);
for (int i = 0; i < 5; i++) {
threadPool.submit(()->{
try {
log.info("就绪,等待主线程信号");
// 挂起线程,等待主线程信号
countDownLatch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}finally {
log.info("已完成...");
}
});
}
Thread.sleep(2000);
log.info("主线程准备完毕..");
countDownLatch.countDown();
}
}
控制台输出如下
10:41:52.079 [pool-1-thread-1] INFO com.learning.java.cooperation.CountDownLatchTest - 就绪,等待主线程信号
10:41:52.079 [pool-1-thread-5] INFO com.learning.java.cooperation.CountDownLatchTest - 就绪,等待主线程信号
10:41:52.080 [pool-1-thread-2] INFO com.learning.java.cooperation.CountDownLatchTest - 就绪,等待主线程信号
10:41:52.088 [pool-1-thread-3] INFO com.learning.java.cooperation.CountDownLatchTest - 就绪,等待主线程信号
10:41:52.092 [pool-1-thread-4] INFO com.learning.java.cooperation.CountDownLatchTest - 就绪,等待主线程信号
10:41:54.072 [main] INFO com.learning.java.cooperation.CountDownLatchTest - 主线程准备完毕..
10:41:54.072 [pool-1-thread-1] INFO com.learning.java.cooperation.CountDownLatchTest - 已完成...
10:41:54.072 [pool-1-thread-5] INFO com.learning.java.cooperation.CountDownLatchTest - 已完成...
10:41:54.072 [pool-1-thread-2] INFO com.learning.java.cooperation.CountDownLatchTest - 已完成...
10:41:54.072 [pool-1-thread-3] INFO com.learning.java.cooperation.CountDownLatchTest - 已完成...
10:41:54.072 [pool-1-thread-4] INFO com.learning.java.cooperation.CountDownLatchTest - 已完成...
这种场景一般用于压测, 多个线程准备好之后,一起发送请求给服务器
总结
- 了解
CountDownLatch
两种常见的使用场景: 一等多和多等一 CountDownLatch
实例化的时候需要传入倒数的次数,等到倒数为0的时候,所有调用过await()
方法的线程就会被唤醒CountDownLatch
不能回滚重置,也就是不能重复使用
边栏推荐
- 第一章:minio介绍与安装
- 嵌入式系統學習筆記
- Bash基本功能—多命令顺序执行与管道符
- Steps and common methods of data analysis
- Commonly used operators of spark
- Bishett route - hyperspectral image classification with deep learning in pytorch environment
- BUUCTF闯关日记--[MRCTF2020]Ezpop1
- BUUCTF闯关日记--[网鼎杯 2020 青龙组]AreUSerialz
- Chapter 2: Minio stand-alone version, using the client to back up files
- [pytorch deep learning practice] learning notes section 4 back propagation
猜你喜欢
Chapter 4: Minio's pre signed URLs upload files
Redis series 14 -- redis cluster
Django中使用Mysql数据库
BUUCTF闯关日记--[极客大挑战 2019]HardSQL1
[pytorch deep learning practice] learning notes section III gradient decline
Set colSpan invalidation for TD of table
Pycharm settings
Chapter 8: custom exception return
[lttng learning journey] - environment construction
Tree structure
随机推荐
How many holes are there in string split operation
第八章:自定义异常返回
[lttng learning journey] - before starting
BUUCTF闯关日记--[MRCTF2020]Ezpop1
链表中的双指针——快慢指针
Spark FAQs
NSSCTF-01-[SWPUCTF 2021 新生赛]gift_F12
BUUCTF闯关日记04--[强网杯 2019]随便注1
人类群星网站收集计划--Michael Kerrisk
条件判断.
Multithreading 05 -- lock
Bash基本功能—历史命令与补全
第二章:minio单机版,使用客户端备份文件
Seata 初识
[lttng learning journey] - lttng features
pytorch 自定义数据集载入(标签在csv文件里)
Multithreading 03 -- synchronized and lock escalation
Redis series 13 -- redis Sentinel
第六章:easyCode代码生成器
软件包管理—RPM包管理—校验和文件提取