CountDownLatch 深度解析
涉及到到函数
public CountDownLatch(int count) {
if (count < 0) throw new IllegalArgumentException("count < 0");
this.sync = new Sync(count);
}
private static final class Sync extends AbstractQueuedSynchronizer {
private static final long serialVersionUID = 4982264981922014374L;
Sync(int count) {
setState(count);
}
int getCount() {
return getState();
}
protected int tryAcquireShared(int acquires) {
return (getState() == 0) ? 1 : -1; // 全到时返回1,未全到时返回-1
}
// 比如是这样一个场景:
// 一个组长等待三个员工,这个函数作用就是修改state变量
// 对应于这个事例就是修改已到达到员工数
protected boolean tryReleaseShared(int releases) {
for (;;) {
int c = getState();
if (c == 0)
return false;
int nextc = c-1;
if (