CountDownLatch 源码分析示例,2021Android进阶者的新篇章
作者:互联网
构造函数内部,初始化一个Sync(count)
//java.util.concurrent.CountDownLatch
public CountDownLatch(int count) {
this.sync = new Sync(count);
}
private static final class Sync extends AbstractQueuedSynchronizer {
Sync(int count) {
//AQS中的state值,充当计数器
setState(count);
}
}
1.图解AQS框架
2.AQS内部类Node
属性介绍
3.countDown()
方法里面做了什么?
//java.util.concurrent.CountDownLatch
public void countDown() {
sync.releaseShared(1);
}
//java.util.concurrent.locks.AbstractQueuedSynchronizer
public final boolean releaseShared(int arg) {
//AQS里面的tryReleaseShared需要子类覆写
if (tryReleaseShared(arg)) {
//state为0的时候,去唤醒等待队列中的线程
doReleaseShared();
return true;
}
return false;
}
//java.util.concurrent.CountDownLatch.Sync
protected boolean tryReleaseShared(int releases) {
for (;
标签:node,Node,进阶,示例,pred,waitStatus,源码,null,节点 来源: https://blog.csdn.net/m0_65145113/article/details/121903717