进入构造方法
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
;
}
}