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