当前位置:网站首页>图解基于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源码分享

边栏推荐
- squid代理服务器
- Codeforces Round #722 (Div. 2)
- 100 important knowledge points that SQL must master: sorting and retrieving data
- SQL必需掌握的100个重要知识点:组合 WHERE 子句
- Serveur mandataire SQUID
- Go from entry to practice - multiple selection and timeout control (notes)
- Codeforces Round #717 (Div. 2)
- Navicat Premium连接问题--- Host ‘xxxxxxxx‘ is not allowed to connect to this MySQL server
- Save method of JPA stepping pit series
- TypeScript学习
猜你喜欢

银河麒麟系统局域网文件共享教程

Animal breeding production virtual simulation teaching system | Sinovel interactive

White whoring red team goby & POC, how do you call white whoring?

"Good voice" has been singing for 10 years. How can the Chinese language in the starry sky sing well in HKEx?

今晚战码先锋润和赛道第2期直播丨如何参与OpenHarmony代码贡献

创建对象时JVM内存结构

VMware vSphere ESXi 7.0安装教程

GoLand永久激活

Flexible IP network test tool -- x-launch

数据平台调度升级改造 | 从Azkaban 平滑过度到Apache DolphinScheduler 的操作实践
随机推荐
Release of global Unicorn list in 2021: the full list of 301 Unicorn enterprises in China is coming!
Love math experiment | Issue 8 - building of Singapore house price prediction model
Very comprehensive dolphin scheduler installation and use documents
Go从入门到实战——所有任务完成(笔记)
关于异常处理的知识整理
ARCS模型介绍
Show the comprehensive strength of strong products, and make the first show of 2022 Lincoln aviator in Southwest China
空指针异常
Go from entry to practice - dependency management (notes)
非常全面的DolphinScheduler(海豚调度)安装使用文档
Acwing周赛57-数字操作-(思维+分解质因数)
Oracle的CTAS能不能将约束等属性带到新表?
SQL必需掌握的100个重要知识点:过滤数据
Navicat Premium连接问题--- Host ‘xxxxxxxx‘ is not allowed to connect to this MySQL server
100 important knowledge points that SQL must master: combining where clauses
Flexible IP network test tool -- x-launch
Flutter隐藏AppBar的返回按钮
Yu Wenwen, Hu Xia and other stars take you to play with the party. Pipi app ignites your summer
Experience Navicat premium 16, unlimited reset, 14 day trial method (with source code)
Use the storcli tool to configure raid. Just collect this article