2
|
1 |
/*
|
|
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
3 |
*
|
|
4 |
* This code is free software; you can redistribute it and/or modify it
|
|
5 |
* under the terms of the GNU General Public License version 2 only, as
|
|
6 |
* published by the Free Software Foundation. Sun designates this
|
|
7 |
* particular file as subject to the "Classpath" exception as provided
|
|
8 |
* by Sun in the LICENSE file that accompanied this code.
|
|
9 |
*
|
|
10 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
11 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
12 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
13 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
14 |
* accompanied this code).
|
|
15 |
*
|
|
16 |
* You should have received a copy of the GNU General Public License version
|
|
17 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
18 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
19 |
*
|
|
20 |
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
|
|
21 |
* CA 95054 USA or visit www.sun.com if you need additional information or
|
|
22 |
* have any questions.
|
|
23 |
*/
|
|
24 |
|
|
25 |
/*
|
|
26 |
* This file is available under and governed by the GNU General Public
|
|
27 |
* License version 2 only, as published by the Free Software Foundation.
|
|
28 |
* However, the following notice accompanied the original version of this
|
|
29 |
* file:
|
|
30 |
*
|
|
31 |
* Written by Doug Lea with assistance from members of JCP JSR-166
|
|
32 |
* Expert Group and released to the public domain, as explained at
|
|
33 |
* http://creativecommons.org/licenses/publicdomain
|
|
34 |
*/
|
|
35 |
|
|
36 |
package java.util.concurrent;
|
|
37 |
import java.util.concurrent.locks.*;
|
|
38 |
import java.util.concurrent.atomic.*;
|
|
39 |
|
|
40 |
/**
|
|
41 |
* A synchronization aid that allows one or more threads to wait until
|
|
42 |
* a set of operations being performed in other threads completes.
|
|
43 |
*
|
|
44 |
* <p>A {@code CountDownLatch} is initialized with a given <em>count</em>.
|
|
45 |
* The {@link #await await} methods block until the current count reaches
|
|
46 |
* zero due to invocations of the {@link #countDown} method, after which
|
|
47 |
* all waiting threads are released and any subsequent invocations of
|
|
48 |
* {@link #await await} return immediately. This is a one-shot phenomenon
|
|
49 |
* -- the count cannot be reset. If you need a version that resets the
|
|
50 |
* count, consider using a {@link CyclicBarrier}.
|
|
51 |
*
|
|
52 |
* <p>A {@code CountDownLatch} is a versatile synchronization tool
|
|
53 |
* and can be used for a number of purposes. A
|
|
54 |
* {@code CountDownLatch} initialized with a count of one serves as a
|
|
55 |
* simple on/off latch, or gate: all threads invoking {@link #await await}
|
|
56 |
* wait at the gate until it is opened by a thread invoking {@link
|
|
57 |
* #countDown}. A {@code CountDownLatch} initialized to <em>N</em>
|
|
58 |
* can be used to make one thread wait until <em>N</em> threads have
|
|
59 |
* completed some action, or some action has been completed N times.
|
|
60 |
*
|
|
61 |
* <p>A useful property of a {@code CountDownLatch} is that it
|
|
62 |
* doesn't require that threads calling {@code countDown} wait for
|
|
63 |
* the count to reach zero before proceeding, it simply prevents any
|
|
64 |
* thread from proceeding past an {@link #await await} until all
|
|
65 |
* threads could pass.
|
|
66 |
*
|
|
67 |
* <p><b>Sample usage:</b> Here is a pair of classes in which a group
|
|
68 |
* of worker threads use two countdown latches:
|
|
69 |
* <ul>
|
|
70 |
* <li>The first is a start signal that prevents any worker from proceeding
|
|
71 |
* until the driver is ready for them to proceed;
|
|
72 |
* <li>The second is a completion signal that allows the driver to wait
|
|
73 |
* until all workers have completed.
|
|
74 |
* </ul>
|
|
75 |
*
|
|
76 |
* <pre>
|
|
77 |
* class Driver { // ...
|
|
78 |
* void main() throws InterruptedException {
|
|
79 |
* CountDownLatch startSignal = new CountDownLatch(1);
|
|
80 |
* CountDownLatch doneSignal = new CountDownLatch(N);
|
|
81 |
*
|
|
82 |
* for (int i = 0; i < N; ++i) // create and start threads
|
|
83 |
* new Thread(new Worker(startSignal, doneSignal)).start();
|
|
84 |
*
|
|
85 |
* doSomethingElse(); // don't let run yet
|
|
86 |
* startSignal.countDown(); // let all threads proceed
|
|
87 |
* doSomethingElse();
|
|
88 |
* doneSignal.await(); // wait for all to finish
|
|
89 |
* }
|
|
90 |
* }
|
|
91 |
*
|
|
92 |
* class Worker implements Runnable {
|
|
93 |
* private final CountDownLatch startSignal;
|
|
94 |
* private final CountDownLatch doneSignal;
|
|
95 |
* Worker(CountDownLatch startSignal, CountDownLatch doneSignal) {
|
|
96 |
* this.startSignal = startSignal;
|
|
97 |
* this.doneSignal = doneSignal;
|
|
98 |
* }
|
|
99 |
* public void run() {
|
|
100 |
* try {
|
|
101 |
* startSignal.await();
|
|
102 |
* doWork();
|
|
103 |
* doneSignal.countDown();
|
|
104 |
* } catch (InterruptedException ex) {} // return;
|
|
105 |
* }
|
|
106 |
*
|
|
107 |
* void doWork() { ... }
|
|
108 |
* }
|
|
109 |
*
|
|
110 |
* </pre>
|
|
111 |
*
|
|
112 |
* <p>Another typical usage would be to divide a problem into N parts,
|
|
113 |
* describe each part with a Runnable that executes that portion and
|
|
114 |
* counts down on the latch, and queue all the Runnables to an
|
|
115 |
* Executor. When all sub-parts are complete, the coordinating thread
|
|
116 |
* will be able to pass through await. (When threads must repeatedly
|
|
117 |
* count down in this way, instead use a {@link CyclicBarrier}.)
|
|
118 |
*
|
|
119 |
* <pre>
|
|
120 |
* class Driver2 { // ...
|
|
121 |
* void main() throws InterruptedException {
|
|
122 |
* CountDownLatch doneSignal = new CountDownLatch(N);
|
|
123 |
* Executor e = ...
|
|
124 |
*
|
|
125 |
* for (int i = 0; i < N; ++i) // create and start threads
|
|
126 |
* e.execute(new WorkerRunnable(doneSignal, i));
|
|
127 |
*
|
|
128 |
* doneSignal.await(); // wait for all to finish
|
|
129 |
* }
|
|
130 |
* }
|
|
131 |
*
|
|
132 |
* class WorkerRunnable implements Runnable {
|
|
133 |
* private final CountDownLatch doneSignal;
|
|
134 |
* private final int i;
|
|
135 |
* WorkerRunnable(CountDownLatch doneSignal, int i) {
|
|
136 |
* this.doneSignal = doneSignal;
|
|
137 |
* this.i = i;
|
|
138 |
* }
|
|
139 |
* public void run() {
|
|
140 |
* try {
|
|
141 |
* doWork(i);
|
|
142 |
* doneSignal.countDown();
|
|
143 |
* } catch (InterruptedException ex) {} // return;
|
|
144 |
* }
|
|
145 |
*
|
|
146 |
* void doWork() { ... }
|
|
147 |
* }
|
|
148 |
*
|
|
149 |
* </pre>
|
|
150 |
*
|
|
151 |
* <p>Memory consistency effects: Actions in a thread prior to calling
|
|
152 |
* {@code countDown()}
|
|
153 |
* <a href="package-summary.html#MemoryVisibility"><i>happen-before</i></a>
|
|
154 |
* actions following a successful return from a corresponding
|
|
155 |
* {@code await()} in another thread.
|
|
156 |
*
|
|
157 |
* @since 1.5
|
|
158 |
* @author Doug Lea
|
|
159 |
*/
|
|
160 |
public class CountDownLatch {
|
|
161 |
/**
|
|
162 |
* Synchronization control For CountDownLatch.
|
|
163 |
* Uses AQS state to represent count.
|
|
164 |
*/
|
|
165 |
private static final class Sync extends AbstractQueuedSynchronizer {
|
|
166 |
private static final long serialVersionUID = 4982264981922014374L;
|
|
167 |
|
|
168 |
Sync(int count) {
|
|
169 |
setState(count);
|
|
170 |
}
|
|
171 |
|
|
172 |
int getCount() {
|
|
173 |
return getState();
|
|
174 |
}
|
|
175 |
|
|
176 |
protected int tryAcquireShared(int acquires) {
|
|
177 |
return getState() == 0? 1 : -1;
|
|
178 |
}
|
|
179 |
|
|
180 |
protected boolean tryReleaseShared(int releases) {
|
|
181 |
// Decrement count; signal when transition to zero
|
|
182 |
for (;;) {
|
|
183 |
int c = getState();
|
|
184 |
if (c == 0)
|
|
185 |
return false;
|
|
186 |
int nextc = c-1;
|
|
187 |
if (compareAndSetState(c, nextc))
|
|
188 |
return nextc == 0;
|
|
189 |
}
|
|
190 |
}
|
|
191 |
}
|
|
192 |
|
|
193 |
private final Sync sync;
|
|
194 |
|
|
195 |
/**
|
|
196 |
* Constructs a {@code CountDownLatch} initialized with the given count.
|
|
197 |
*
|
|
198 |
* @param count the number of times {@link #countDown} must be invoked
|
|
199 |
* before threads can pass through {@link #await}
|
|
200 |
* @throws IllegalArgumentException if {@code count} is negative
|
|
201 |
*/
|
|
202 |
public CountDownLatch(int count) {
|
|
203 |
if (count < 0) throw new IllegalArgumentException("count < 0");
|
|
204 |
this.sync = new Sync(count);
|
|
205 |
}
|
|
206 |
|
|
207 |
/**
|
|
208 |
* Causes the current thread to wait until the latch has counted down to
|
|
209 |
* zero, unless the thread is {@linkplain Thread#interrupt interrupted}.
|
|
210 |
*
|
|
211 |
* <p>If the current count is zero then this method returns immediately.
|
|
212 |
*
|
|
213 |
* <p>If the current count is greater than zero then the current
|
|
214 |
* thread becomes disabled for thread scheduling purposes and lies
|
|
215 |
* dormant until one of two things happen:
|
|
216 |
* <ul>
|
|
217 |
* <li>The count reaches zero due to invocations of the
|
|
218 |
* {@link #countDown} method; or
|
|
219 |
* <li>Some other thread {@linkplain Thread#interrupt interrupts}
|
|
220 |
* the current thread.
|
|
221 |
* </ul>
|
|
222 |
*
|
|
223 |
* <p>If the current thread:
|
|
224 |
* <ul>
|
|
225 |
* <li>has its interrupted status set on entry to this method; or
|
|
226 |
* <li>is {@linkplain Thread#interrupt interrupted} while waiting,
|
|
227 |
* </ul>
|
|
228 |
* then {@link InterruptedException} is thrown and the current thread's
|
|
229 |
* interrupted status is cleared.
|
|
230 |
*
|
|
231 |
* @throws InterruptedException if the current thread is interrupted
|
|
232 |
* while waiting
|
|
233 |
*/
|
|
234 |
public void await() throws InterruptedException {
|
|
235 |
sync.acquireSharedInterruptibly(1);
|
|
236 |
}
|
|
237 |
|
|
238 |
/**
|
|
239 |
* Causes the current thread to wait until the latch has counted down to
|
|
240 |
* zero, unless the thread is {@linkplain Thread#interrupt interrupted},
|
|
241 |
* or the specified waiting time elapses.
|
|
242 |
*
|
|
243 |
* <p>If the current count is zero then this method returns immediately
|
|
244 |
* with the value {@code true}.
|
|
245 |
*
|
|
246 |
* <p>If the current count is greater than zero then the current
|
|
247 |
* thread becomes disabled for thread scheduling purposes and lies
|
|
248 |
* dormant until one of three things happen:
|
|
249 |
* <ul>
|
|
250 |
* <li>The count reaches zero due to invocations of the
|
|
251 |
* {@link #countDown} method; or
|
|
252 |
* <li>Some other thread {@linkplain Thread#interrupt interrupts}
|
|
253 |
* the current thread; or
|
|
254 |
* <li>The specified waiting time elapses.
|
|
255 |
* </ul>
|
|
256 |
*
|
|
257 |
* <p>If the count reaches zero then the method returns with the
|
|
258 |
* value {@code true}.
|
|
259 |
*
|
|
260 |
* <p>If the current thread:
|
|
261 |
* <ul>
|
|
262 |
* <li>has its interrupted status set on entry to this method; or
|
|
263 |
* <li>is {@linkplain Thread#interrupt interrupted} while waiting,
|
|
264 |
* </ul>
|
|
265 |
* then {@link InterruptedException} is thrown and the current thread's
|
|
266 |
* interrupted status is cleared.
|
|
267 |
*
|
|
268 |
* <p>If the specified waiting time elapses then the value {@code false}
|
|
269 |
* is returned. If the time is less than or equal to zero, the method
|
|
270 |
* will not wait at all.
|
|
271 |
*
|
|
272 |
* @param timeout the maximum time to wait
|
|
273 |
* @param unit the time unit of the {@code timeout} argument
|
|
274 |
* @return {@code true} if the count reached zero and {@code false}
|
|
275 |
* if the waiting time elapsed before the count reached zero
|
|
276 |
* @throws InterruptedException if the current thread is interrupted
|
|
277 |
* while waiting
|
|
278 |
*/
|
|
279 |
public boolean await(long timeout, TimeUnit unit)
|
|
280 |
throws InterruptedException {
|
|
281 |
return sync.tryAcquireSharedNanos(1, unit.toNanos(timeout));
|
|
282 |
}
|
|
283 |
|
|
284 |
/**
|
|
285 |
* Decrements the count of the latch, releasing all waiting threads if
|
|
286 |
* the count reaches zero.
|
|
287 |
*
|
|
288 |
* <p>If the current count is greater than zero then it is decremented.
|
|
289 |
* If the new count is zero then all waiting threads are re-enabled for
|
|
290 |
* thread scheduling purposes.
|
|
291 |
*
|
|
292 |
* <p>If the current count equals zero then nothing happens.
|
|
293 |
*/
|
|
294 |
public void countDown() {
|
|
295 |
sync.releaseShared(1);
|
|
296 |
}
|
|
297 |
|
|
298 |
/**
|
|
299 |
* Returns the current count.
|
|
300 |
*
|
|
301 |
* <p>This method is typically used for debugging and testing purposes.
|
|
302 |
*
|
|
303 |
* @return the current count
|
|
304 |
*/
|
|
305 |
public long getCount() {
|
|
306 |
return sync.getCount();
|
|
307 |
}
|
|
308 |
|
|
309 |
/**
|
|
310 |
* Returns a string identifying this latch, as well as its state.
|
|
311 |
* The state, in brackets, includes the String {@code "Count ="}
|
|
312 |
* followed by the current count.
|
|
313 |
*
|
|
314 |
* @return a string identifying this latch, as well as its state
|
|
315 |
*/
|
|
316 |
public String toString() {
|
|
317 |
return super.toString() + "[Count = " + sync.getCount() + "]";
|
|
318 |
}
|
|
319 |
}
|