author | dl |
Wed, 21 Dec 2016 14:26:52 -0800 | |
changeset 42927 | 1d31e540bfcb |
parent 42322 | c3474fef4fe4 |
permissions | -rw-r--r-- |
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 |
|
5506 | 6 |
* published by the Free Software Foundation. Oracle designates this |
2 | 7 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 8 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 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 |
* |
|
5506 | 20 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
21 |
* or visit www.oracle.com if you need additional information or have any |
|
22 |
* questions. |
|
2 | 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 |
|
9242
ef138d47df58
7034657: Update Creative Commons license URL in legal notices
dl
parents:
5506
diff
changeset
|
33 |
* http://creativecommons.org/publicdomain/zero/1.0/ |
2 | 34 |
*/ |
35 |
||
36 |
package java.util.concurrent; |
|
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
37 |
|
14325
622c473a21aa
8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents:
9242
diff
changeset
|
38 |
import java.util.concurrent.locks.Condition; |
622c473a21aa
8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents:
9242
diff
changeset
|
39 |
import java.util.concurrent.locks.ReentrantLock; |
2 | 40 |
|
41 |
/** |
|
42 |
* A synchronization aid that allows a set of threads to all wait for |
|
43 |
* each other to reach a common barrier point. CyclicBarriers are |
|
44 |
* useful in programs involving a fixed sized party of threads that |
|
45 |
* must occasionally wait for each other. The barrier is called |
|
46 |
* <em>cyclic</em> because it can be re-used after the waiting threads |
|
47 |
* are released. |
|
48 |
* |
|
18768 | 49 |
* <p>A {@code CyclicBarrier} supports an optional {@link Runnable} command |
2 | 50 |
* that is run once per barrier point, after the last thread in the party |
51 |
* arrives, but before any threads are released. |
|
52 |
* This <em>barrier action</em> is useful |
|
53 |
* for updating shared-state before any of the parties continue. |
|
54 |
* |
|
18768 | 55 |
* <p><b>Sample usage:</b> Here is an example of using a barrier in a |
56 |
* parallel decomposition design: |
|
14325
622c473a21aa
8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents:
9242
diff
changeset
|
57 |
* |
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
58 |
* <pre> {@code |
2 | 59 |
* class Solver { |
60 |
* final int N; |
|
61 |
* final float[][] data; |
|
62 |
* final CyclicBarrier barrier; |
|
63 |
* |
|
64 |
* class Worker implements Runnable { |
|
65 |
* int myRow; |
|
66 |
* Worker(int row) { myRow = row; } |
|
67 |
* public void run() { |
|
68 |
* while (!done()) { |
|
69 |
* processRow(myRow); |
|
70 |
* |
|
71 |
* try { |
|
72 |
* barrier.await(); |
|
73 |
* } catch (InterruptedException ex) { |
|
74 |
* return; |
|
75 |
* } catch (BrokenBarrierException ex) { |
|
76 |
* return; |
|
77 |
* } |
|
78 |
* } |
|
79 |
* } |
|
80 |
* } |
|
81 |
* |
|
82 |
* public Solver(float[][] matrix) { |
|
83 |
* data = matrix; |
|
84 |
* N = matrix.length; |
|
42322
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
40817
diff
changeset
|
85 |
* Runnable barrierAction = () -> mergeRows(...); |
18768 | 86 |
* barrier = new CyclicBarrier(N, barrierAction); |
2 | 87 |
* |
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
88 |
* List<Thread> threads = new ArrayList<>(N); |
18768 | 89 |
* for (int i = 0; i < N; i++) { |
90 |
* Thread thread = new Thread(new Worker(i)); |
|
91 |
* threads.add(thread); |
|
92 |
* thread.start(); |
|
93 |
* } |
|
94 |
* |
|
95 |
* // wait until done |
|
96 |
* for (Thread thread : threads) |
|
97 |
* thread.join(); |
|
2 | 98 |
* } |
14325
622c473a21aa
8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents:
9242
diff
changeset
|
99 |
* }}</pre> |
622c473a21aa
8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents:
9242
diff
changeset
|
100 |
* |
2 | 101 |
* Here, each worker thread processes a row of the matrix then waits at the |
102 |
* barrier until all rows have been processed. When all rows are processed |
|
103 |
* the supplied {@link Runnable} barrier action is executed and merges the |
|
104 |
* rows. If the merger |
|
18768 | 105 |
* determines that a solution has been found then {@code done()} will return |
106 |
* {@code true} and each worker will terminate. |
|
2 | 107 |
* |
108 |
* <p>If the barrier action does not rely on the parties being suspended when |
|
109 |
* it is executed, then any of the threads in the party could execute that |
|
110 |
* action when it is released. To facilitate this, each invocation of |
|
111 |
* {@link #await} returns the arrival index of that thread at the barrier. |
|
112 |
* You can then choose which thread should execute the barrier action, for |
|
113 |
* example: |
|
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
114 |
* <pre> {@code |
14325
622c473a21aa
8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents:
9242
diff
changeset
|
115 |
* if (barrier.await() == 0) { |
622c473a21aa
8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents:
9242
diff
changeset
|
116 |
* // log the completion of this iteration |
622c473a21aa
8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents:
9242
diff
changeset
|
117 |
* }}</pre> |
2 | 118 |
* |
18768 | 119 |
* <p>The {@code CyclicBarrier} uses an all-or-none breakage model |
2 | 120 |
* for failed synchronization attempts: If a thread leaves a barrier |
121 |
* point prematurely because of interruption, failure, or timeout, all |
|
122 |
* other threads waiting at that barrier point will also leave |
|
123 |
* abnormally via {@link BrokenBarrierException} (or |
|
124 |
* {@link InterruptedException} if they too were interrupted at about |
|
125 |
* the same time). |
|
126 |
* |
|
127 |
* <p>Memory consistency effects: Actions in a thread prior to calling |
|
128 |
* {@code await()} |
|
129 |
* <a href="package-summary.html#MemoryVisibility"><i>happen-before</i></a> |
|
130 |
* actions that are part of the barrier action, which in turn |
|
131 |
* <i>happen-before</i> actions following a successful return from the |
|
132 |
* corresponding {@code await()} in other threads. |
|
133 |
* |
|
134 |
* @see CountDownLatch |
|
135 |
* |
|
136 |
* @author Doug Lea |
|
40817
4f5fb115676d
8164169: Miscellaneous changes imported from jsr166 CVS 2016-09
dl
parents:
32991
diff
changeset
|
137 |
* @since 1.5 |
2 | 138 |
*/ |
139 |
public class CyclicBarrier { |
|
140 |
/** |
|
141 |
* Each use of the barrier is represented as a generation instance. |
|
142 |
* The generation changes whenever the barrier is tripped, or |
|
143 |
* is reset. There can be many generations associated with threads |
|
144 |
* using the barrier - due to the non-deterministic way the lock |
|
145 |
* may be allocated to waiting threads - but only one of these |
|
18768 | 146 |
* can be active at a time (the one to which {@code count} applies) |
2 | 147 |
* and all the rest are either broken or tripped. |
148 |
* There need not be an active generation if there has been a break |
|
149 |
* but no subsequent reset. |
|
150 |
*/ |
|
151 |
private static class Generation { |
|
42927
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
42322
diff
changeset
|
152 |
Generation() {} // prevent access constructor creation |
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
42322
diff
changeset
|
153 |
boolean broken; // initially false |
2 | 154 |
} |
155 |
||
156 |
/** The lock for guarding barrier entry */ |
|
157 |
private final ReentrantLock lock = new ReentrantLock(); |
|
158 |
/** Condition to wait on until tripped */ |
|
159 |
private final Condition trip = lock.newCondition(); |
|
160 |
/** The number of parties */ |
|
161 |
private final int parties; |
|
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
162 |
/** The command to run when tripped */ |
2 | 163 |
private final Runnable barrierCommand; |
164 |
/** The current generation */ |
|
165 |
private Generation generation = new Generation(); |
|
166 |
||
167 |
/** |
|
168 |
* Number of parties still waiting. Counts down from parties to 0 |
|
169 |
* on each generation. It is reset to parties on each new |
|
170 |
* generation or when broken. |
|
171 |
*/ |
|
172 |
private int count; |
|
173 |
||
174 |
/** |
|
175 |
* Updates state on barrier trip and wakes up everyone. |
|
176 |
* Called only while holding lock. |
|
177 |
*/ |
|
178 |
private void nextGeneration() { |
|
179 |
// signal completion of last generation |
|
180 |
trip.signalAll(); |
|
181 |
// set up next generation |
|
182 |
count = parties; |
|
183 |
generation = new Generation(); |
|
184 |
} |
|
185 |
||
186 |
/** |
|
187 |
* Sets current barrier generation as broken and wakes up everyone. |
|
188 |
* Called only while holding lock. |
|
189 |
*/ |
|
190 |
private void breakBarrier() { |
|
191 |
generation.broken = true; |
|
192 |
count = parties; |
|
193 |
trip.signalAll(); |
|
194 |
} |
|
195 |
||
196 |
/** |
|
197 |
* Main barrier code, covering the various policies. |
|
198 |
*/ |
|
199 |
private int dowait(boolean timed, long nanos) |
|
200 |
throws InterruptedException, BrokenBarrierException, |
|
201 |
TimeoutException { |
|
202 |
final ReentrantLock lock = this.lock; |
|
203 |
lock.lock(); |
|
204 |
try { |
|
205 |
final Generation g = generation; |
|
206 |
||
207 |
if (g.broken) |
|
208 |
throw new BrokenBarrierException(); |
|
209 |
||
210 |
if (Thread.interrupted()) { |
|
211 |
breakBarrier(); |
|
212 |
throw new InterruptedException(); |
|
213 |
} |
|
214 |
||
14325
622c473a21aa
8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents:
9242
diff
changeset
|
215 |
int index = --count; |
622c473a21aa
8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents:
9242
diff
changeset
|
216 |
if (index == 0) { // tripped |
622c473a21aa
8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents:
9242
diff
changeset
|
217 |
boolean ranAction = false; |
622c473a21aa
8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents:
9242
diff
changeset
|
218 |
try { |
622c473a21aa
8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents:
9242
diff
changeset
|
219 |
final Runnable command = barrierCommand; |
622c473a21aa
8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents:
9242
diff
changeset
|
220 |
if (command != null) |
622c473a21aa
8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents:
9242
diff
changeset
|
221 |
command.run(); |
622c473a21aa
8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents:
9242
diff
changeset
|
222 |
ranAction = true; |
622c473a21aa
8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents:
9242
diff
changeset
|
223 |
nextGeneration(); |
622c473a21aa
8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents:
9242
diff
changeset
|
224 |
return 0; |
622c473a21aa
8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents:
9242
diff
changeset
|
225 |
} finally { |
622c473a21aa
8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents:
9242
diff
changeset
|
226 |
if (!ranAction) |
622c473a21aa
8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents:
9242
diff
changeset
|
227 |
breakBarrier(); |
622c473a21aa
8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents:
9242
diff
changeset
|
228 |
} |
622c473a21aa
8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents:
9242
diff
changeset
|
229 |
} |
2 | 230 |
|
231 |
// loop until tripped, broken, interrupted, or timed out |
|
232 |
for (;;) { |
|
233 |
try { |
|
234 |
if (!timed) |
|
235 |
trip.await(); |
|
236 |
else if (nanos > 0L) |
|
237 |
nanos = trip.awaitNanos(nanos); |
|
238 |
} catch (InterruptedException ie) { |
|
239 |
if (g == generation && ! g.broken) { |
|
240 |
breakBarrier(); |
|
241 |
throw ie; |
|
242 |
} else { |
|
243 |
// We're about to finish waiting even if we had not |
|
244 |
// been interrupted, so this interrupt is deemed to |
|
245 |
// "belong" to subsequent execution. |
|
246 |
Thread.currentThread().interrupt(); |
|
247 |
} |
|
248 |
} |
|
249 |
||
250 |
if (g.broken) |
|
251 |
throw new BrokenBarrierException(); |
|
252 |
||
253 |
if (g != generation) |
|
254 |
return index; |
|
255 |
||
256 |
if (timed && nanos <= 0L) { |
|
257 |
breakBarrier(); |
|
258 |
throw new TimeoutException(); |
|
259 |
} |
|
260 |
} |
|
261 |
} finally { |
|
262 |
lock.unlock(); |
|
263 |
} |
|
264 |
} |
|
265 |
||
266 |
/** |
|
18768 | 267 |
* Creates a new {@code CyclicBarrier} that will trip when the |
2 | 268 |
* given number of parties (threads) are waiting upon it, and which |
269 |
* will execute the given barrier action when the barrier is tripped, |
|
270 |
* performed by the last thread entering the barrier. |
|
271 |
* |
|
272 |
* @param parties the number of threads that must invoke {@link #await} |
|
273 |
* before the barrier is tripped |
|
274 |
* @param barrierAction the command to execute when the barrier is |
|
275 |
* tripped, or {@code null} if there is no action |
|
276 |
* @throws IllegalArgumentException if {@code parties} is less than 1 |
|
277 |
*/ |
|
278 |
public CyclicBarrier(int parties, Runnable barrierAction) { |
|
279 |
if (parties <= 0) throw new IllegalArgumentException(); |
|
280 |
this.parties = parties; |
|
281 |
this.count = parties; |
|
282 |
this.barrierCommand = barrierAction; |
|
283 |
} |
|
284 |
||
285 |
/** |
|
18768 | 286 |
* Creates a new {@code CyclicBarrier} that will trip when the |
2 | 287 |
* given number of parties (threads) are waiting upon it, and |
288 |
* does not perform a predefined action when the barrier is tripped. |
|
289 |
* |
|
290 |
* @param parties the number of threads that must invoke {@link #await} |
|
291 |
* before the barrier is tripped |
|
292 |
* @throws IllegalArgumentException if {@code parties} is less than 1 |
|
293 |
*/ |
|
294 |
public CyclicBarrier(int parties) { |
|
295 |
this(parties, null); |
|
296 |
} |
|
297 |
||
298 |
/** |
|
299 |
* Returns the number of parties required to trip this barrier. |
|
300 |
* |
|
301 |
* @return the number of parties required to trip this barrier |
|
302 |
*/ |
|
303 |
public int getParties() { |
|
304 |
return parties; |
|
305 |
} |
|
306 |
||
307 |
/** |
|
308 |
* Waits until all {@linkplain #getParties parties} have invoked |
|
18768 | 309 |
* {@code await} on this barrier. |
2 | 310 |
* |
311 |
* <p>If the current thread is not the last to arrive then it is |
|
312 |
* disabled for thread scheduling purposes and lies dormant until |
|
313 |
* one of the following things happens: |
|
314 |
* <ul> |
|
315 |
* <li>The last thread arrives; or |
|
316 |
* <li>Some other thread {@linkplain Thread#interrupt interrupts} |
|
317 |
* the current thread; or |
|
318 |
* <li>Some other thread {@linkplain Thread#interrupt interrupts} |
|
319 |
* one of the other waiting threads; or |
|
320 |
* <li>Some other thread times out while waiting for barrier; or |
|
321 |
* <li>Some other thread invokes {@link #reset} on this barrier. |
|
322 |
* </ul> |
|
323 |
* |
|
324 |
* <p>If the current thread: |
|
325 |
* <ul> |
|
326 |
* <li>has its interrupted status set on entry to this method; or |
|
327 |
* <li>is {@linkplain Thread#interrupt interrupted} while waiting |
|
328 |
* </ul> |
|
329 |
* then {@link InterruptedException} is thrown and the current thread's |
|
330 |
* interrupted status is cleared. |
|
331 |
* |
|
332 |
* <p>If the barrier is {@link #reset} while any thread is waiting, |
|
333 |
* or if the barrier {@linkplain #isBroken is broken} when |
|
18768 | 334 |
* {@code await} is invoked, or while any thread is waiting, then |
2 | 335 |
* {@link BrokenBarrierException} is thrown. |
336 |
* |
|
337 |
* <p>If any thread is {@linkplain Thread#interrupt interrupted} while waiting, |
|
338 |
* then all other waiting threads will throw |
|
339 |
* {@link BrokenBarrierException} and the barrier is placed in the broken |
|
340 |
* state. |
|
341 |
* |
|
342 |
* <p>If the current thread is the last thread to arrive, and a |
|
343 |
* non-null barrier action was supplied in the constructor, then the |
|
344 |
* current thread runs the action before allowing the other threads to |
|
345 |
* continue. |
|
346 |
* If an exception occurs during the barrier action then that exception |
|
347 |
* will be propagated in the current thread and the barrier is placed in |
|
348 |
* the broken state. |
|
349 |
* |
|
350 |
* @return the arrival index of the current thread, where index |
|
18768 | 351 |
* {@code getParties() - 1} indicates the first |
2 | 352 |
* to arrive and zero indicates the last to arrive |
353 |
* @throws InterruptedException if the current thread was interrupted |
|
354 |
* while waiting |
|
355 |
* @throws BrokenBarrierException if <em>another</em> thread was |
|
356 |
* interrupted or timed out while the current thread was |
|
357 |
* waiting, or the barrier was reset, or the barrier was |
|
358 |
* broken when {@code await} was called, or the barrier |
|
18768 | 359 |
* action (if present) failed due to an exception |
2 | 360 |
*/ |
361 |
public int await() throws InterruptedException, BrokenBarrierException { |
|
362 |
try { |
|
363 |
return dowait(false, 0L); |
|
364 |
} catch (TimeoutException toe) { |
|
14325
622c473a21aa
8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents:
9242
diff
changeset
|
365 |
throw new Error(toe); // cannot happen |
2 | 366 |
} |
367 |
} |
|
368 |
||
369 |
/** |
|
370 |
* Waits until all {@linkplain #getParties parties} have invoked |
|
18768 | 371 |
* {@code await} on this barrier, or the specified waiting time elapses. |
2 | 372 |
* |
373 |
* <p>If the current thread is not the last to arrive then it is |
|
374 |
* disabled for thread scheduling purposes and lies dormant until |
|
375 |
* one of the following things happens: |
|
376 |
* <ul> |
|
377 |
* <li>The last thread arrives; or |
|
378 |
* <li>The specified timeout elapses; or |
|
379 |
* <li>Some other thread {@linkplain Thread#interrupt interrupts} |
|
380 |
* the current thread; or |
|
381 |
* <li>Some other thread {@linkplain Thread#interrupt interrupts} |
|
382 |
* one of the other waiting threads; or |
|
383 |
* <li>Some other thread times out while waiting for barrier; or |
|
384 |
* <li>Some other thread invokes {@link #reset} on this barrier. |
|
385 |
* </ul> |
|
386 |
* |
|
387 |
* <p>If the current thread: |
|
388 |
* <ul> |
|
389 |
* <li>has its interrupted status set on entry to this method; or |
|
390 |
* <li>is {@linkplain Thread#interrupt interrupted} while waiting |
|
391 |
* </ul> |
|
392 |
* then {@link InterruptedException} is thrown and the current thread's |
|
393 |
* interrupted status is cleared. |
|
394 |
* |
|
395 |
* <p>If the specified waiting time elapses then {@link TimeoutException} |
|
396 |
* is thrown. If the time is less than or equal to zero, the |
|
397 |
* method will not wait at all. |
|
398 |
* |
|
399 |
* <p>If the barrier is {@link #reset} while any thread is waiting, |
|
400 |
* or if the barrier {@linkplain #isBroken is broken} when |
|
18768 | 401 |
* {@code await} is invoked, or while any thread is waiting, then |
2 | 402 |
* {@link BrokenBarrierException} is thrown. |
403 |
* |
|
404 |
* <p>If any thread is {@linkplain Thread#interrupt interrupted} while |
|
405 |
* waiting, then all other waiting threads will throw {@link |
|
406 |
* BrokenBarrierException} and the barrier is placed in the broken |
|
407 |
* state. |
|
408 |
* |
|
409 |
* <p>If the current thread is the last thread to arrive, and a |
|
410 |
* non-null barrier action was supplied in the constructor, then the |
|
411 |
* current thread runs the action before allowing the other threads to |
|
412 |
* continue. |
|
413 |
* If an exception occurs during the barrier action then that exception |
|
414 |
* will be propagated in the current thread and the barrier is placed in |
|
415 |
* the broken state. |
|
416 |
* |
|
417 |
* @param timeout the time to wait for the barrier |
|
418 |
* @param unit the time unit of the timeout parameter |
|
419 |
* @return the arrival index of the current thread, where index |
|
18768 | 420 |
* {@code getParties() - 1} indicates the first |
2 | 421 |
* to arrive and zero indicates the last to arrive |
422 |
* @throws InterruptedException if the current thread was interrupted |
|
423 |
* while waiting |
|
18768 | 424 |
* @throws TimeoutException if the specified timeout elapses. |
425 |
* In this case the barrier will be broken. |
|
2 | 426 |
* @throws BrokenBarrierException if <em>another</em> thread was |
427 |
* interrupted or timed out while the current thread was |
|
428 |
* waiting, or the barrier was reset, or the barrier was broken |
|
429 |
* when {@code await} was called, or the barrier action (if |
|
18768 | 430 |
* present) failed due to an exception |
2 | 431 |
*/ |
432 |
public int await(long timeout, TimeUnit unit) |
|
433 |
throws InterruptedException, |
|
434 |
BrokenBarrierException, |
|
435 |
TimeoutException { |
|
436 |
return dowait(true, unit.toNanos(timeout)); |
|
437 |
} |
|
438 |
||
439 |
/** |
|
440 |
* Queries if this barrier is in a broken state. |
|
441 |
* |
|
442 |
* @return {@code true} if one or more parties broke out of this |
|
443 |
* barrier due to interruption or timeout since |
|
444 |
* construction or the last reset, or a barrier action |
|
445 |
* failed due to an exception; {@code false} otherwise. |
|
446 |
*/ |
|
447 |
public boolean isBroken() { |
|
448 |
final ReentrantLock lock = this.lock; |
|
449 |
lock.lock(); |
|
450 |
try { |
|
451 |
return generation.broken; |
|
452 |
} finally { |
|
453 |
lock.unlock(); |
|
454 |
} |
|
455 |
} |
|
456 |
||
457 |
/** |
|
458 |
* Resets the barrier to its initial state. If any parties are |
|
459 |
* currently waiting at the barrier, they will return with a |
|
460 |
* {@link BrokenBarrierException}. Note that resets <em>after</em> |
|
461 |
* a breakage has occurred for other reasons can be complicated to |
|
462 |
* carry out; threads need to re-synchronize in some other way, |
|
463 |
* and choose one to perform the reset. It may be preferable to |
|
464 |
* instead create a new barrier for subsequent use. |
|
465 |
*/ |
|
466 |
public void reset() { |
|
467 |
final ReentrantLock lock = this.lock; |
|
468 |
lock.lock(); |
|
469 |
try { |
|
470 |
breakBarrier(); // break the current generation |
|
471 |
nextGeneration(); // start a new generation |
|
472 |
} finally { |
|
473 |
lock.unlock(); |
|
474 |
} |
|
475 |
} |
|
476 |
||
477 |
/** |
|
478 |
* Returns the number of parties currently waiting at the barrier. |
|
479 |
* This method is primarily useful for debugging and assertions. |
|
480 |
* |
|
481 |
* @return the number of parties currently blocked in {@link #await} |
|
482 |
*/ |
|
483 |
public int getNumberWaiting() { |
|
484 |
final ReentrantLock lock = this.lock; |
|
485 |
lock.lock(); |
|
486 |
try { |
|
487 |
return parties - count; |
|
488 |
} finally { |
|
489 |
lock.unlock(); |
|
490 |
} |
|
491 |
} |
|
492 |
} |