158 * @since 1.5
159 * @author Doug Lea
160 */
161 public class CountDownLatch {
162 /**
163 * Synchronization control For CountDownLatch.
164 * Uses AQS state to represent count.
165 */
166 private static final class Sync extends AbstractQueuedSynchronizer {
167 private static final long serialVersionUID = 4982264981922014374L;
168
169 Sync(int count) {
170 setState(count);
171 }
172
173 int getCount() {
174 return getState();
175 }
176
177 protected int tryAcquireShared(int acquires) {
178 return getState() == 0? 1 : -1;
179 }
180
181 protected boolean tryReleaseShared(int releases) {
182 // Decrement count; signal when transition to zero
183 for (;;) {
184 int c = getState();
185 if (c == 0)
186 return false;
187 int nextc = c-1;
188 if (compareAndSetState(c, nextc))
189 return nextc == 0;
190 }
191 }
192 }
193
194 private final Sync sync;
195
196 /**
197 * Constructs a {@code CountDownLatch} initialized with the given count.
198 *
|
158 * @since 1.5
159 * @author Doug Lea
160 */
161 public class CountDownLatch {
162 /**
163 * Synchronization control For CountDownLatch.
164 * Uses AQS state to represent count.
165 */
166 private static final class Sync extends AbstractQueuedSynchronizer {
167 private static final long serialVersionUID = 4982264981922014374L;
168
169 Sync(int count) {
170 setState(count);
171 }
172
173 int getCount() {
174 return getState();
175 }
176
177 protected int tryAcquireShared(int acquires) {
178 return (getState() == 0) ? 1 : -1;
179 }
180
181 protected boolean tryReleaseShared(int releases) {
182 // Decrement count; signal when transition to zero
183 for (;;) {
184 int c = getState();
185 if (c == 0)
186 return false;
187 int nextc = c-1;
188 if (compareAndSetState(c, nextc))
189 return nextc == 0;
190 }
191 }
192 }
193
194 private final Sync sync;
195
196 /**
197 * Constructs a {@code CountDownLatch} initialized with the given count.
198 *
|