当前位置:网站首页>Source code analysis of countdownlatch of AQS
Source code analysis of countdownlatch of AQS
2022-07-28 06:45:00 【yfyh2021】
- Introduce
CountDownLatch( atresia ) Is a synchronization assistance class , Allow one or more threads to wait , Until other threads complete the operation set .
- Code preparation
- Let multiple threads wait for one thread to execute
public class CountDownLatchTest1 {
public static void main(String[] args) throws InterruptedException {
CountDownLatch countDownLatch = new CountDownLatch(1);
for(int i=0;i<5;i++){
new Thread(()->{
try {
countDownLatch.await();
String parter = "【" + Thread.currentThread().getName() + "】";
System.out.println(parter + " Start execution ……");
} catch (InterruptedException e) {
e.printStackTrace();
}
}).start();
}
Thread.sleep(2000);
System.out.println(" wait for ");
countDownLatch.countDown();
}
}
- Let a thread wait for multiple threads to execute
public class CountDownLatchTest2 {
public static void main(String[] args) throws InterruptedException {
CountDownLatch countDownLatch = new CountDownLatch(5);
for(int i=0;i<5;i++){
final int index =i;
new Thread(()->{
try {
Thread.sleep(1000 +
ThreadLocalRandom.current().nextInt(1000));
System.out.println(Thread.currentThread().getName()
+ " finish task" + index);
countDownLatch.countDown();
} catch (InterruptedException e) {
e.printStackTrace();
}
}).start();
}
System.out.println(" Main thread waiting ");
countDownLatch.await();
System.out.println(" The main thread : After all tasks are completed , Summarize the results ");
}
}

- Source code analysis

Get into await() Methods found acquireSharedInterruptibly() The method is to pass in fixed parameters 1.

again AQS This familiar code , First, try Try , And then there was do Do some queuing and blocking operations . From this method we can see countDownLatch It's a shared lock. .

Latching try The method is quite simple , Its state The value passed in when creating the lock for us . return -1, Enter us AQS Of doAcquireSharedInterruptibly() Method to queue and block .

Latching countDown() Methods are also common to us releaseShared, And fixed transmission 1

among doReleaseShared() Method is AQS Common unlocking method , May refer to semaphore. We need to have a look at tryReleaseShared() Method in countDownLatch How is it implemented .

It can be seen that every lock is every countdown Will be state Minus one , When state by 0 Will wake up the thread .
- summary
- We can see in the reentrantlock in state The attribute is the number of reentries , But in semaphore Can be understood as the amount of resources , stay countDownLatch Can be understood as counter can know . Exclusive locks are reentrant , Shared locks do not support reentry .
- We see countDownLatch The function is similar join() Method . But locking is relative to join It will be more flexible , It can be found in n Thread countDown operation , It can also be done in a thread n this countDown operation . and join The implementation principle of is to keep checking join Is the thread alive or not , If join Thread survival will make the current thread wait forever .
边栏推荐
猜你喜欢

STM32的IAP跳转相关bug经历

Explain in detail
![[basic knowledge of binary tree]](/img/4f/9fc27a30b958af26537c299e150ee9.png)
[basic knowledge of binary tree]
![[pta-- use queues to solve the problem of monkeys choosing kings]](/img/54/94359fb3557ac07f7786ecf61a5409.png)
[pta-- use queues to solve the problem of monkeys choosing kings]

SSAO By Computer Shader(一)

Mongodb quick start

2022-06-07 responsebodyadvice caused the spring frame problem in swagger

valgrind工具

Leetcode 刷题日记 剑指 Offer II 047. 二叉树剪枝

图形管线基础(番外篇)
随机推荐
AQS之countDownLatch源码分析
[PTA----树的遍历]
OJ 1451 digital games
C语言的动态内存管理函数
Two dimensional array practice: spiral matrix
2022-06-07 VI. log implementation
[C note] data type and storage
OJ 1129 fraction matrix
[dynamic planning -- the best period series for buying and selling stocks]
explain详解
OJ 1045 反转然后相加
Problem solving for ACM freshmen in Jiangzhong on October 26
ZOJ Problem 1005 jugs
What are the open earphones? Four types of air conduction earphones with excellent sound quality are recommended
SSAO by computer shader (I)
ZOJ Problem 1005 jugs
【动态规划--买卖股票的最佳时期系列】
气传导蓝牙耳机怎么样、最值得入手的气传导耳机
OJ 1507 deletion problem
Leetcode 刷题日记 剑指 Offer II 055. 二叉搜索树迭代器