当前位置:网站首页>Juc-7.3-thread collaboration cyclicbarrier
Juc-7.3-thread collaboration cyclicbarrier
2022-07-22 21:11:00 【Hehe 1112】
Statement
CyclicBarrier
Loop fence is also a thread collaboration tool , and CountDownLatch
It's like , Can block a set of threads
effect
When a large number of threads cooperate with each other , Calculate different tasks separately , And when the final unified summary is needed , We can use CyclicBarrier
,CyclicBarrier
You can construct a staging point , When a thread executes await()
, It will go to the assembly point and wait , Until all threads reach the assembly point , Then the fence will be removed , All threads start again , Continue with the remaining tasks .
Common methods
- Constructors , There are two parameters , The first is the number of waiting threads , The second is a
Runnable
The implementation of the await()
: The calling thread enters the blocking state and waits
Code example
@Slf4j
public class CyclicBarrierDemo {
public static void main(String[] args) {
CyclicBarrier cyclicBarrier = new CyclicBarrier(5, () -> log.info(" All threads are here ..."));
for (int i = 0; i < 5; i++) {
new Thread(new Task(i + 1, cyclicBarrier)).start();
}
}
@AllArgsConstructor
@Slf4j
static class Task implements Runnable{
private int id;
private CyclicBarrier cyclicBarrier;
@Override
public void run() {
log.info(" The current thread is executing ,id:[{}]", id);
try {
Thread.sleep((long) (Math.random() * 10000));
cyclicBarrier.await();
log.info(" All threads are here , Carry on , Current thread id:{}",id);
} catch (InterruptedException | BrokenBarrierException e) {
e.printStackTrace();
}
}
}
}
The console output is as follows :
10:40:34.645 [Thread-1] INFO com.learning.java.cooperation.CyclicBarrierDemo$Task - The current thread is executing ,id:[2]
10:40:34.645 [Thread-0] INFO com.learning.java.cooperation.CyclicBarrierDemo$Task - The current thread is executing ,id:[1]
10:40:34.645 [Thread-2] INFO com.learning.java.cooperation.CyclicBarrierDemo$Task - The current thread is executing ,id:[3]
10:40:34.645 [Thread-3] INFO com.learning.java.cooperation.CyclicBarrierDemo$Task - The current thread is executing ,id:[4]
10:40:34.645 [Thread-4] INFO com.learning.java.cooperation.CyclicBarrierDemo$Task - The current thread is executing ,id:[5]
10:40:43.843 [Thread-4] INFO com.learning.java.cooperation.CyclicBarrierDemo - All threads are here ...
10:40:43.843 [Thread-4] INFO com.learning.java.cooperation.CyclicBarrierDemo$Task - All threads are here , Carry on , Current thread id:5
10:40:43.843 [Thread-2] INFO com.learning.java.cooperation.CyclicBarrierDemo$Task - All threads are here , Carry on , Current thread id:3
10:40:43.843 [Thread-0] INFO com.learning.java.cooperation.CyclicBarrierDemo$Task - All threads are here , Carry on , Current thread id:1
10:40:43.843 [Thread-1] INFO com.learning.java.cooperation.CyclicBarrierDemo$Task - All threads are here , Carry on , Current thread id:2
10:40:43.843 [Thread-3] INFO com.learning.java.cooperation.CyclicBarrierDemo$Task - All threads are here , Carry on , Current thread id:4
First of all, declare that a quantity is 5 Of CyclicBarrier
, Then build 5 Threads , Call later await()
Method , wait until 5 All of them await()
After method , Will execute the Runnable
The implementation of the , Then all threads wake up , Carry on
contrast CountDownLatch
- The effect is different :
CyclicBarrier
Wait until a fixed number of threads have reached the fence position to continue execution ,CountDownLatch
Just wait until the counter is 0 You can continue to execute , in other wordsCountDownLatch
For events , andCyclicBarrier
Is for threads - Reusability is different :
CountDownLatch
After the countdown triggers the latch to open, it can no longer be reused , andCyclicBarrier
It's reusable
边栏推荐
猜你喜欢
4.文本编辑器
BUUCTF闯关日记--[MRCTF2020]Ez_bypass1
Chapter 1: Minio introduction and installation
解决微信小程序 thirdScriptError错误
[LTTng学习之旅]------Trace控制--进阶
Buuctf breakthrough diary 03 -- [geek challenge 2019] havefun1
pytorch 自定义数据集载入(标签在csv文件里)
微信小程序Cannot read property 'setData' of null错误
如何做好研发精益需求管理
TIME的新封面:元宇宙将改变一切
随机推荐
BUUCTF闯关日记--[NCTF2019]Fake XML cookbook
软件包管理—RPM包管理—校验和文件提取
Buuctf entry diary -- [mrctf2020] how about you (super detailed)
Bash变量--环境变量
Chapter 7: login using JWT token
[lttng learning journey] - a preliminary study of trace view
第六章:easyCode代码生成器
嵌入式系统学习笔记
第八章:自定义异常返回
Pytorch自动求导
8.内存与磁盘管理
(6) Vulhub column: apereo CAS 4.x deserialization vulnerability
流程控制.
字符处理命令
YOLO9000: Better, Faster, Stronger
JUC-7.0-线程协作-CountDownLatch
2020中南大学信息与通信夏令营面试
Desensitization of mobile phone, name and ID card information in the project in the log and response data
BUUCTF闯关日记04--[ACTF2020 新生赛]Include1
Wechat applet cannot read property'setdata'of null error