当前位置:网站首页>图解基于AQS队列实现的CountDownLatch和CyclicBarrier
图解基于AQS队列实现的CountDownLatch和CyclicBarrier
2022-06-27 19:23:00 【高粱】
CountDownLatch

CountDownLatch 基于 AQS 共享模式实现
简单上手
CountDownLatch 基于 AQS 的共享模式的使用
new CountDownLatch(线程个数);
初始化对应线程
每个子线程run方法里调用 countDownLatch.countDown();
最后主线程调用 countDownLatch.await();
实现效果: 主线程会等待所有子线程执行结束才往下执行
countDownLatch.countDown()
countDownLatch.countDown() 方法会把AQS的status -1
此时子线程把status值改变后就结束了并没有睡眠
会继续执行下面子线程其他的逻辑(countDown方法一般放在子线程执行逻辑最后,
如果放在前面会导致主线程先于子线程执行逻辑,即导致countDownLatch失效)
countDownLatch.await()
countDownLatch.await() 方法先判断status减到 0 没有就一直阻塞睡眠等待
当所有子线程都调用了 countDownLatch.countDown() 后最后一个子线程唤醒主线程
CyclicBarrier


CyclicBarrier 基于 Condition 和 ReenTrantLock实现
情景预热
CountDownLatch有几个问题:首先CountDownLatch在await之后必须依靠别的线程来给它countDown,打开门闩;
其次CountDownLatch在countDown到0之后,该CountDownLatch的生命周期就结束了,它不能重用。
那么有没有既可以自己给自己打开门闩而且还能重用的呢,有的,那就是CyclicBarrier,译作回环栅栏
存在一个计数变量 parties
CyclicBarrier的使用通俗来说就是
有一个栅栏 CyclicBarrier ,它必须由n(parties)个人才能被推到
推到之后这n个人(线程)才能出来
出来之后呢,再把这个栅栏重新立起来又可以用了(重用)
简单上手
new CyclicBarrier (线程个数,所有子线程执行完后主线程执行的任务);
初始化对应线程
每个子线程run方法里调用cyclicBarrier.await();
实现效果: 所有子线程执行到同一个等待点, 并且等待点可以重新利用
CyclicBarrier属性
lock(ReentrantLock)它是用来给CyclicBarrier的操作加锁的
trip(Condition)用来实现CyclicBarrier的wait和notify的
parties(int)计数值,相当于CountDownLatch的count
barrierCommand(Runnable)当CyclicBarrier打开后,要执行的任务
generation(Generation)CyclicBarrier的代,用来实现CyclicBarrier的重用
count(int)值等于parties,每次有线程进入时,count值减一,减到0的时候CyclicBarrier打开,随后count值被reset为parties
重点dowait源码分享

边栏推荐
- Navicat Premium连接问题--- Host ‘xxxxxxxx‘ is not allowed to connect to this MySQL server
- 请教CMS小程序首页的幻灯片在哪里设置?
- Cerebral cortex: predicting children's mathematical skills from task state and resting state brain function connections
- uniapp拦截请求
- A set of system to reduce 10 times the traffic pressure in crowded areas
- Here are 12 commonly used function formulas for you. All used ones are good
- MySQL客户端工具推荐,一定想不到最好用巨然是它
- mysql使用笔记一
- SQL Server for循环用法
- Contest 2050 and Codeforces Round #718 (Div. 1 + Div. 2)
猜你喜欢

squid代理服务器

通过CE修改器修改大型网络游戏

让马化腾失望了!Web3.0,毫无希望

Ceph分布式存储

GoLand永久激活

JPA踩坑系列之save方法

Serveur mandataire SQUID

Go从入门到实战——channel的关闭和广播(笔记)

Flood fighting and disaster relief, overcoming difficulties, and City United premium products rushed to the aid of Yingde to donate loving materials

Let Ma Huateng down! Web3.0, hopeless
随机推荐
Share an experience of self positioning + problem solving
MySQL usage notes 1
SQL Server for循环用法
MYSQL 性能优化 index 函数,隐藏,前缀,hash 索引 使用方法(2)
"Good voice" has been singing for 10 years. How can the Chinese language in the starry sky sing well in HKEx?
MySQL客户端工具推荐,一定想不到最好用巨然是它
空指针异常
Love number experiment | Issue 7 - Financial Crisis Analysis Based on random forest
Codeforces Round #717 (Div. 2)
Let Ma Huateng down! Web3.0, hopeless
送你12个常用函数公式,用过的都说好
How to do a good job of gateway high availability protection in the big promotion scenario
MySQL Express - day 1 - basic introduction
银河麒麟系统局域网文件共享教程
Go from entry to practice - dependency management (notes)
Sharing | intelligent environmental protection - ecological civilization informatization solution (PDF attached)
01-Golang-环境搭建
Flexible IP network test tool -- x-launch
100 important knowledge points that SQL must master: creating calculation fields
关于异常处理的知识整理