当前位置:网站首页>Juc-7.0-thread collaboration countdownlatch
Juc-7.0-thread collaboration countdownlatch
2022-07-22 21:11:00 【Hehe 1112】
Statement
The running and timing of threads are beyond our control , In some cases, we may need multiple threads to complete some tasks , Like threads A etc. B The thread runs after execution
CountDownLatch
CountDownLatch
Is a tool class for thread collaboration , It's a counter , The following methods are mainly used :
- Construction method , Parameter is
count
, Is the value that needs to be counted backwards await()
: callawait()
Method will be suspended , wait until count by 0 Will continue to execute whencountDown()
: Each call will count Value reduction 1, Until 0 The waiting thread will be awakened
Common use
Use a
CountDownLatch
The first use of is that a thread waits for multiple threads to complete execution , Continue your work , The code example is as follows
@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(" Completed ...");
countDownLatch.countDown();
}
});
}
log.info(" Waiting for other threads ....");
countDownLatch.await();
log.info(" All threads are finished ...");
}
}
The operation output is as follows :
10:25:42.359 [main] INFO com.learning.java.cooperation.CountDownLatchTest - Waiting for other threads ....
10:25:43.352 [pool-1-thread-2] INFO com.learning.java.cooperation.CountDownLatchTest - Completed ...
10:25:43.352 [pool-1-thread-3] INFO com.learning.java.cooperation.CountDownLatchTest - Completed ...
10:25:43.352 [pool-1-thread-1] INFO com.learning.java.cooperation.CountDownLatchTest - Completed ...
10:25:43.353 [pool-1-thread-5] INFO com.learning.java.cooperation.CountDownLatchTest - Completed ...
10:25:43.353 [pool-1-thread-4] INFO com.learning.java.cooperation.CountDownLatchTest - Completed ...
10:25:43.353 [main] INFO com.learning.java.cooperation.CountDownLatchTest - All threads are finished ...
First, create a new one count by 5 Of CountDownLatch
Counter , Then build 5 Threads , Then the main thread calls await()
wait for , wait for 5 All threads have called countDown()
after , count by 0, The main thread continues to run
Use two
The second usage is that multiple threads wait for the signal of a thread , Then start executing at the same time , The code example is as follows :
@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(" be ready , Wait for the main thread signal ");
// Hung thread , Wait for the main thread signal
countDownLatch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}finally {
log.info(" Completed ...");
}
});
}
Thread.sleep(2000);
log.info(" The main thread is ready ..");
countDownLatch.countDown();
}
}
The console output is as follows
10:41:52.079 [pool-1-thread-1] INFO com.learning.java.cooperation.CountDownLatchTest - be ready , Wait for the main thread signal
10:41:52.079 [pool-1-thread-5] INFO com.learning.java.cooperation.CountDownLatchTest - be ready , Wait for the main thread signal
10:41:52.080 [pool-1-thread-2] INFO com.learning.java.cooperation.CountDownLatchTest - be ready , Wait for the main thread signal
10:41:52.088 [pool-1-thread-3] INFO com.learning.java.cooperation.CountDownLatchTest - be ready , Wait for the main thread signal
10:41:52.092 [pool-1-thread-4] INFO com.learning.java.cooperation.CountDownLatchTest - be ready , Wait for the main thread signal
10:41:54.072 [main] INFO com.learning.java.cooperation.CountDownLatchTest - The main thread is ready ..
10:41:54.072 [pool-1-thread-1] INFO com.learning.java.cooperation.CountDownLatchTest - Completed ...
10:41:54.072 [pool-1-thread-5] INFO com.learning.java.cooperation.CountDownLatchTest - Completed ...
10:41:54.072 [pool-1-thread-2] INFO com.learning.java.cooperation.CountDownLatchTest - Completed ...
10:41:54.072 [pool-1-thread-3] INFO com.learning.java.cooperation.CountDownLatchTest - Completed ...
10:41:54.072 [pool-1-thread-4] INFO com.learning.java.cooperation.CountDownLatchTest - Completed ...
This scenario is generally used for pressure measurement , When multiple threads are ready , Send the request to the server together
summary
- understand
CountDownLatch
Two common usage scenarios : Wait more and wait more CountDownLatch
When instantiating, you need to pass in the count down , Wait until the countdown is 0 When , All calledawait()
The thread of the method will be awakenedCountDownLatch
Cannot roll back reset , That is, it cannot be reused
边栏推荐
猜你喜欢
Multithreading 04 -- atomicity of threads, CAS
第二章:给项目配置数据源,redis,security,swagger等工具jar
Chapter 7: login using JWT token
BUUCTF闯关日记--[SUCTF 2019]CheckIn1()
MATLAB2017a环境下使用libsvm-3.23出现的问题与解决方案
[pytorch deep learning practice] learning notes section 4 back propagation
Buctf passing Diary - - [Netting Cup 2020 Qinglong Group] areuserialz
Bishett route - hyperspectral image classification with deep learning in pytorch environment
Chapter 2: Minio stand-alone version, using the client to back up files
Multithreading 04 -- order of threads
随机推荐
第一章:minio介绍与安装
mysql 连接查询在on中使用convert 导致扫描行数增长
Pytorch custom data set loading (label in CSV file)
Using MySQL database in Django
Multithreading 04 -- order of threads
Seata first met
Redis series 15 -- redis cache cleaning
第六章:easyCode代码生成器
Bash变量--位置参数变量
[GXYCTF2019]BabyUpload1
如何做好研发精益需求管理
[lttng learning journey] - trace control -- advanced
第四章:minio的presigned URLs上传文件
MVCC
Django中使用Mysql数据库
[LTTng实操]------设计一套东西监控某周期运行用户程序的执行时间和周期--需求分析和方案设计
Redis series 14 -- redis cluster
Multithreading 06 -- countdownlatch, cyclicbarrier, semaphore
PKG config lookup library and for compilation
When the project goes online, the old data needs to be modified, and writing SQL is too troublesome. Look at the powerful function of Excel with simple SQL