当前位置:网站首页>Figure countdownlatch and cyclicbarrier based on AQS queue
Figure countdownlatch and cyclicbarrier based on AQS queue
2022-06-27 21:57:00 【sorghum】
CountDownLatch

CountDownLatch be based on AQS Sharing mode implementation
Simple to fit
CountDownLatch be based on AQS The use of Shared patterns
new CountDownLatch( Number of threads );
Initialize the corresponding thread
Each child thread run Method call countDownLatch.countDown();
Finally, the main thread calls countDownLatch.await();
Realization effect : The main thread will wait for all child threads to finish executing before proceeding
countDownLatch.countDown()
countDownLatch.countDown() Method will bring AQS Of status -1
At this time, the sub thread will status After value changes, there is no sleep
It will continue to execute other logic of the following sub threads (countDown Methods are usually placed at the end of the sub thread execution logic ,
If it is placed in the front, it will cause the main thread to execute logic before the sub thread , That leads to countDownLatch invalid )
countDownLatch.await()
countDownLatch.await() Method first judge status Reduced to 0 No, I have been blocking my sleep and waiting
When all child threads call countDownLatch.countDown() The last child thread wakes up the main thread
CyclicBarrier


CyclicBarrier be based on Condition and ReenTrantLock Realization
Warm up scenario
CountDownLatch There are several problems : First CountDownLatch stay await Then you have to rely on other threads to give it countDown, Open the latch ;
secondly CountDownLatch stay countDown To 0 after , The CountDownLatch The life cycle of , It cannot be reused .
So is there something that you can unlock yourself and reuse , yes , we have , That's it CyclicBarrier, Loop fence
There is a count variable parties
CyclicBarrier Generally speaking, the use of is
There is a fence CyclicBarrier , It must be n(parties) Individuals can be pushed to
Later here n personal ( Threads ) To come out
After coming out , The fence can be used again ( reusing )
Simple to fit
new CyclicBarrier ( Number of threads , The task executed by the main thread after all the sub threads are executed );
Initialize the corresponding thread
Each child thread run Method call cyclicBarrier.await();
Realization effect : All child threads execute to the same wait point , And waiting points can be reused
CyclicBarrier attribute
lock(ReentrantLock) It is used to give CyclicBarrier The operation is locked
trip(Condition) Used to implement CyclicBarrier Of wait and notify Of
parties(int) Count value , amount to CountDownLatch Of count
barrierCommand(Runnable) When CyclicBarrier After opening , Tasks to perform
generation(Generation)CyclicBarrier Generation of , Used to implement CyclicBarrier Reuse of
count(int) The value is equal to parties, Every time a thread enters ,count Minus one , Reduced to 0 When CyclicBarrier open , And then count Value by reset by parties
a key dowait The source code to share

边栏推荐
- 【MySQL】数据库函数通关教程下篇(窗口函数专题)
- GBase 8a V8版本节点替换期间通过并发数控制资源使用减少对系统影响的方法
- STM32CubeIDE1.9.0\STM32CubeMX 6.5 F429IGT6加LAN8720A,配置ETH+LWIP
- 流程控制任务
- Go从入门到实战——行为的定义和实现(笔记)
- Simulink导出FMU模型文件方法
- \w和[A-Za-z0-9_],\d和[0-9]等价吗?
- Go from introduction to actual combat - package (notes)
- Go from introduction to practice - polymorphism (note)
- 跟我一起AQS SOS AQS
猜你喜欢

STM32CubeIDE1.9.0\STM32CubeMX 6.5 F429IGT6加LAN8720A,配置ETH+LWIP

Go from introduction to actual combat - context and task cancellation (notes)

熊市慢慢,Bit.Store提供稳定Staking产品助你穿越牛熊

Go从入门到实战——CSP并发机制(笔记)

使用Fiddler模拟弱网测试(2G/3G)

Stm32cubeide1.9.0\stm32cubemx 6.5 f429igt6 plus lan8720a, configure eth+lwip

Burp suite遇到的常见问题

不外泄的测试用例设计秘籍--模块测试
How to design an elegant caching function

我想我要开始写我自己的博客了。
随机推荐
STM32F107+LAN8720A使用STM32cubeMX配置网络连接+tcp主从机+UDP app
Method of reading file contents by Excel
The difference between scrum and Kanban
Bit. Store: long bear market, stable stacking products may become the main theme
[LeetCode]513. 找树左下角的值
Go from entry to practice - dependency management (notes)
Go from introduction to actual combat - all tasks completed (notes)
Go from introduction to actual combat -- channel closing and broadcasting (notes)
Example of using gbase 8A OLAP function group by grouping sets
Magic POI error in reading excel template file
Slow bear market, bit Store provides stable stacking products to help you cross the bull and bear
Go从入门到实战——Context与任务取消(笔记)
[leetcode] 508. Élément de sous - arbre le plus fréquent et
软件测试自动化测试之——接口测试从入门到精通,每天学习一点点
I think I should start writing my own blog.
GBase 8a OLAP函数group by grouping sets的使用样例
Go从入门到实战——接口(笔记)
Go从入门到实战——Panic和recover(笔记)
使用Fiddler模拟弱网测试(2G/3G)
qt base64加解密