当前位置:网站首页>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

边栏推荐
猜你喜欢

真香,自从用了Charles,Fiddler已经被我彻底卸载了

【Redis】零基础十分钟学会Redis

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

VMware virtual machine PE startup

Go from introduction to actual combat -- channel closing and broadcasting (notes)

Go from entry to practice - dependency management (notes)

C language programming detailed version (learning note 1) I can't understand it after reading, and I can't help it.

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

win11桌面出现“了解此图片”如何删除

Go从入门到实战——共享内存并发机制(笔记)
随机推荐
熊市慢慢,Bit.Store提供稳定Staking产品助你穿越牛熊
数组作业题
How to delete "know this picture" on win11 desktop
[Sword Offer II]剑指 Offer II 029. 排序的循环链表
Xiao Wang's interview training task
Process control task
快速excel导出
At 19:00 on Tuesday evening, the 8th live broadcast of battle code Pioneer - how to participate in openharmony's open source contribution in multiple directions
美团20k软件测试工程师的经验分享
如何做好功能测试?你确定不想知道吗?
专题教程——选队长游戏
[LeetCode]动态规划解分割数组I[Red Fox]
Array assignment
读写分离-Mysql的主从复制
清华大学教授:软件测试已经走入一个误区——“非代码不可”
Go从入门到实战——CSP并发机制(笔记)
Gbase 8A OLAP analysis function cume_ Example of dist
Gbase 8A method for reducing the impact on the system by controlling resource usage through concurrency during node replacement of V8 version
Go 访问GBase 8a 数据库的一个方法
Sharing | intelligent environmental protection - ecological civilization informatization solution (PDF attached)