ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/main/java/util/concurrent/CyclicBarrier.java
Revision: 1.60
Committed: Sat Feb 2 04:02:49 2019 UTC (5 years, 4 months ago) by jsr166
Branch: MAIN
Changes since 1.59: +5 -6 lines
Log Message:
add a comma for clarity

File Contents

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