CountDownLatch笔记

进入构造方法
public CountDownLatch( int count) {
if (count < 0 ) throw new IllegalArgumentException( "count < 0" );
//又是一个锁,又是 state = newState也就 count的值
this . sync = new Sync(count);
}

先进入 countDownLatch.await()看看

public void await() throws InterruptedException {
sync .acquireSharedInterruptibly( 1 );
}

在进入
public final void acquireSharedInterruptibly( int arg)
throws InterruptedException {
if (Thread. interrupted ())
throw new InterruptedException();
if (tryAcquireShared(arg) < 0 )
doAcquireSharedInterruptibly(arg);
}
进入

protected int tryAcquireShared( int acquires) {
//也就是state线程全到达了为0,返回1,否则-1
return (getState() == 0 ) ? 1 : - 1 ;
}

返回 acquireSharedInterruptibly
当返回-1线程没有全部到达
进入 doAcquireSharedInterruptibly这里的都在 AbstractQueuedSynchronizer
private void doAcquireSharedInterruptibly( int arg)
throws InterruptedException {
final Node node = addWaiter(Node. SHARED );
boolean failed = true ;
try {
for (;;) {
final Node p = node.predecessor();
if (p == head ) {
// tryAcquireShared这个是 (getState() == 0 ) ? 1 : - 1看看线程是否全部到达
int r = tryAcquireShared(arg);
if (r >= 0 ) {
//这边可以进去释放了
setHeadAndPropagate(node, r);
p. next = null ; // help GC
failed = false ;
return ;
}
}
if ( shouldParkAfterFailedAcquire (p, node) &&
parkAndCheckInterrupt())
throw new InterruptedException();
}
} finally {
if (failed)
cancelAcquire(node);
}
}

进入 shouldParkAfterFailedAcquire又在 AbstractQueuedSynchronizer里面了

private static boolean shouldParkAfterFailedAcquire(Node pred, Node node) {
int ws = pred. waitStatus ;
if (ws == Node. SIGNAL )
/*
* This node has already set status asking a release
* to signal it, so it can safely park.
*/
return true ;
if (ws > 0 ) {
/*
* Predecessor was cancelled. Skip over predecessors and
* indicate retry.
*/
do {
node. prev = pred = pred. prev ;
} while (pred. waitStatus > 0 );
pred. next = node;
} else {
/*
* waitStatus must be 0 or PROPAGATE. Indicate that we
* need a signal, but don't park yet. Caller will need to
* retry to make sure it cannot acquire before parking.
*/
//这边这个状态0的是因为我们新建了空的node作为head啊之类的,所以状态一定是0或者共享式释放的时候会把他设置成传播 PROPAGATE -3
compareAndSetWaitStatus (pred, ws, Node. SIGNAL );
}
return false ;
}




进入 countDownLatch .countDown()
public void countDown() {
sync .releaseShared( 1 );
}
在进入

public final boolean releaseShared( int arg) {
if (tryReleaseShared(arg)) {
doReleaseShared();
return true ;
}
return false ;
}

//先去 tryReleaseShared看看

protected boolean tryReleaseShared( int releases) {
// Decrement count; signal when transition to zero
for (;;) {
//获得之前设置的state
int c = getState();
//没了就返回false
if (c == 0 )
return false ;
//这边就是 state= state-1
int nextc = c- 1 ;
if (compareAndSetState(c, nextc))
//也就是减完了返回true,否则false
return nextc == 0 ;
}
}

然后返回为true就是线程全到达了进入
doReleaseShared()

private void doReleaseShared() {

for (;;) {
Node h = head ;
if (h != null && h != tail ) {
int ws = h. waitStatus ;
if (ws == Node. SIGNAL ) {
if (! compareAndSetWaitStatus (h, Node. SIGNAL , 0 ))
continue ; // loop to recheck cases
//唤醒线程
unparkSuccessor(h);
}
//设为传播没发现有什么具体作用,应该是做个标记吧
else if (ws == 0 &&
! compareAndSetWaitStatus (h, 0 , Node. PROPAGATE ))
continue ; // loop on failed CAS
}
if (h == head ) // loop if head changed
break ;
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值