233 |
|
* performed by the last thread entering the barrier. |
234 |
|
* |
235 |
|
* @param parties the number of threads that must invoke {@link #await} |
236 |
< |
* before the barrier is tripped. |
236 |
> |
* before the barrier is tripped |
237 |
|
* @param barrierAction the command to execute when the barrier is |
238 |
< |
* tripped, or <tt>null</tt> if there is no action. |
239 |
< |
* |
240 |
< |
* @throws IllegalArgumentException if <tt>parties</tt> is less than 1. |
238 |
> |
* tripped, or {@code null} if there is no action |
239 |
> |
* @throws IllegalArgumentException if {@code parties} is less than 1 |
240 |
|
*/ |
241 |
|
public CyclicBarrier(int parties, Runnable barrierAction) { |
242 |
|
if (parties <= 0) throw new IllegalArgumentException(); |
251 |
|
* does not perform a predefined action when the barrier is tripped. |
252 |
|
* |
253 |
|
* @param parties the number of threads that must invoke {@link #await} |
254 |
< |
* before the barrier is tripped. |
255 |
< |
* |
257 |
< |
* @throws IllegalArgumentException if <tt>parties</tt> is less than 1. |
254 |
> |
* before the barrier is tripped |
255 |
> |
* @throws IllegalArgumentException if {@code parties} is less than 1 |
256 |
|
*/ |
257 |
|
public CyclicBarrier(int parties) { |
258 |
|
this(parties, null); |
260 |
|
|
261 |
|
/** |
262 |
|
* Returns the number of parties required to trip this barrier. |
263 |
< |
* @return the number of parties required to trip this barrier. |
263 |
> |
* |
264 |
> |
* @return the number of parties required to trip this barrier |
265 |
|
*/ |
266 |
|
public int getParties() { |
267 |
|
return parties; |
268 |
|
} |
269 |
|
|
270 |
|
/** |
271 |
< |
* Waits until all {@link #getParties parties} have invoked <tt>await</tt> |
272 |
< |
* on this barrier. |
271 |
> |
* Waits until all {@linkplain #getParties parties} have invoked |
272 |
> |
* <tt>await</tt> on this barrier. |
273 |
|
* |
274 |
|
* <p>If the current thread is not the last to arrive then it is |
275 |
|
* disabled for thread scheduling purposes and lies dormant until |
276 |
|
* one of the following things happens: |
277 |
|
* <ul> |
278 |
|
* <li>The last thread arrives; or |
279 |
< |
* <li>Some other thread {@link Thread#interrupt interrupts} the current |
280 |
< |
* thread; or |
281 |
< |
* <li>Some other thread {@link Thread#interrupt interrupts} one of the |
282 |
< |
* other waiting threads; or |
279 |
> |
* <li>Some other thread {@linkplain Thread#interrupt interrupts} |
280 |
> |
* the current thread; or |
281 |
> |
* <li>Some other thread {@linkplain Thread#interrupt interrupts} |
282 |
> |
* one of the other waiting threads; or |
283 |
|
* <li>Some other thread times out while waiting for barrier; or |
284 |
|
* <li>Some other thread invokes {@link #reset} on this barrier. |
285 |
|
* </ul> |
286 |
+ |
* |
287 |
|
* <p>If the current thread: |
288 |
|
* <ul> |
289 |
|
* <li>has its interrupted status set on entry to this method; or |
290 |
< |
* <li>is {@link Thread#interrupt interrupted} while waiting |
290 |
> |
* <li>is {@linkplain Thread#interrupt interrupted} while waiting |
291 |
|
* </ul> |
292 |
|
* then {@link InterruptedException} is thrown and the current thread's |
293 |
|
* interrupted status is cleared. |
294 |
|
* |
295 |
< |
* <p>If the barrier is {@link #reset} while any thread is waiting, or if |
296 |
< |
* the barrier {@link #isBroken is broken} when <tt>await</tt> is invoked, |
297 |
< |
* or while any thread is waiting, |
298 |
< |
* then {@link BrokenBarrierException} is thrown. |
295 |
> |
* <p>If the barrier is {@link #reset} while any thread is waiting, |
296 |
> |
* or if the barrier {@linkplain #isBroken is broken} when |
297 |
> |
* <tt>await</tt> is invoked, or while any thread is waiting, then |
298 |
> |
* {@link BrokenBarrierException} is thrown. |
299 |
|
* |
300 |
< |
* <p>If any thread is {@link Thread#interrupt interrupted} while waiting, |
300 |
> |
* <p>If any thread is {@linkplain Thread#interrupt interrupted} while waiting, |
301 |
|
* then all other waiting threads will throw |
302 |
|
* {@link BrokenBarrierException} and the barrier is placed in the broken |
303 |
|
* state. |
311 |
|
* the broken state. |
312 |
|
* |
313 |
|
* @return the arrival index of the current thread, where index |
314 |
< |
* <tt>{@link #getParties()} - 1</tt> indicates the first to arrive and |
315 |
< |
* zero indicates the last to arrive. |
316 |
< |
* |
314 |
> |
* <tt>{@link #getParties()} - 1</tt> indicates the first |
315 |
> |
* to arrive and zero indicates the last to arrive |
316 |
|
* @throws InterruptedException if the current thread was interrupted |
317 |
< |
* while waiting. |
317 |
> |
* while waiting |
318 |
|
* @throws BrokenBarrierException if <em>another</em> thread was |
319 |
< |
* interrupted or timed out while the current thread was waiting, |
320 |
< |
* or the barrier was reset, or the barrier was broken when |
321 |
< |
* <tt>await</tt> was called, or the barrier action (if present) |
322 |
< |
* failed due an exception. |
319 |
> |
* interrupted or timed out while the current thread was |
320 |
> |
* waiting, or the barrier was reset, or the barrier was |
321 |
> |
* broken when {@code await} was called, or the barrier |
322 |
> |
* action (if present) failed due an exception. |
323 |
|
*/ |
324 |
|
public int await() throws InterruptedException, BrokenBarrierException { |
325 |
|
try { |
330 |
|
} |
331 |
|
|
332 |
|
/** |
333 |
< |
* Waits until all {@link #getParties parties} have invoked <tt>await</tt> |
334 |
< |
* on this barrier. |
333 |
> |
* Waits until all {@linkplain #getParties parties} have invoked |
334 |
> |
* <tt>await</tt> on this barrier, or the specified waiting time elapses. |
335 |
|
* |
336 |
|
* <p>If the current thread is not the last to arrive then it is |
337 |
|
* disabled for thread scheduling purposes and lies dormant until |
339 |
|
* <ul> |
340 |
|
* <li>The last thread arrives; or |
341 |
|
* <li>The specified timeout elapses; or |
342 |
< |
* <li>Some other thread {@link Thread#interrupt interrupts} the current |
343 |
< |
* thread; or |
344 |
< |
* <li>Some other thread {@link Thread#interrupt interrupts} one of the |
345 |
< |
* other waiting threads; or |
342 |
> |
* <li>Some other thread {@linkplain Thread#interrupt interrupts} |
343 |
> |
* the current thread; or |
344 |
> |
* <li>Some other thread {@linkplain Thread#interrupt interrupts} |
345 |
> |
* one of the other waiting threads; or |
346 |
|
* <li>Some other thread times out while waiting for barrier; or |
347 |
|
* <li>Some other thread invokes {@link #reset} on this barrier. |
348 |
|
* </ul> |
349 |
+ |
* |
350 |
|
* <p>If the current thread: |
351 |
|
* <ul> |
352 |
|
* <li>has its interrupted status set on entry to this method; or |
353 |
< |
* <li>is {@link Thread#interrupt interrupted} while waiting |
353 |
> |
* <li>is {@linkplain Thread#interrupt interrupted} while waiting |
354 |
|
* </ul> |
355 |
|
* then {@link InterruptedException} is thrown and the current thread's |
356 |
|
* interrupted status is cleared. |
359 |
|
* is thrown. If the time is less than or equal to zero, the |
360 |
|
* method will not wait at all. |
361 |
|
* |
362 |
< |
* <p>If the barrier is {@link #reset} while any thread is waiting, or if |
363 |
< |
* the barrier {@link #isBroken is broken} when <tt>await</tt> is invoked, |
364 |
< |
* or while any thread is waiting, |
365 |
< |
* then {@link BrokenBarrierException} is thrown. |
366 |
< |
* |
367 |
< |
* <p>If any thread is {@link Thread#interrupt interrupted} while waiting, |
368 |
< |
* then all other waiting threads will throw |
369 |
< |
* {@link BrokenBarrierException} and the barrier is placed in the broken |
362 |
> |
* <p>If the barrier is {@link #reset} while any thread is waiting, |
363 |
> |
* or if the barrier {@linkplain #isBroken is broken} when |
364 |
> |
* <tt>await</tt> is invoked, or while any thread is waiting, then |
365 |
> |
* {@link BrokenBarrierException} is thrown. |
366 |
> |
* |
367 |
> |
* <p>If any thread is {@linkplain Thread#interrupt interrupted} while |
368 |
> |
* waiting, then all other waiting threads will throw {@link |
369 |
> |
* BrokenBarrierException} and the barrier is placed in the broken |
370 |
|
* state. |
371 |
|
* |
372 |
|
* <p>If the current thread is the last thread to arrive, and a |
380 |
|
* @param timeout the time to wait for the barrier |
381 |
|
* @param unit the time unit of the timeout parameter |
382 |
|
* @return the arrival index of the current thread, where index |
383 |
< |
* <tt>{@link #getParties()} - 1</tt> indicates the first to arrive and |
384 |
< |
* zero indicates the last to arrive. |
385 |
< |
* |
383 |
> |
* <tt>{@link #getParties()} - 1</tt> indicates the first |
384 |
> |
* to arrive and zero indicates the last to arrive |
385 |
|
* @throws InterruptedException if the current thread was interrupted |
386 |
< |
* while waiting. |
387 |
< |
* @throws TimeoutException if the specified timeout elapses. |
386 |
> |
* while waiting |
387 |
> |
* @throws TimeoutException if the specified timeout elapses |
388 |
|
* @throws BrokenBarrierException if <em>another</em> thread was |
389 |
< |
* interrupted or timed out while the current thread was waiting, |
390 |
< |
* or the barrier was reset, or the barrier was broken when |
391 |
< |
* <tt>await</tt> was called, or the barrier action (if present) |
392 |
< |
* failed due an exception. |
389 |
> |
* interrupted or timed out while the current thread was |
390 |
> |
* waiting, or the barrier was reset, or the barrier was broken |
391 |
> |
* when {@code await} was called, or the barrier action (if |
392 |
> |
* present) failed due an exception |
393 |
|
*/ |
394 |
|
public int await(long timeout, TimeUnit unit) |
395 |
|
throws InterruptedException, |
400 |
|
|
401 |
|
/** |
402 |
|
* Queries if this barrier is in a broken state. |
403 |
< |
* @return <tt>true</tt> if one or more parties broke out of this |
404 |
< |
* barrier due to interruption or timeout since construction or |
405 |
< |
* the last reset, or a barrier action failed due to an exception; |
406 |
< |
* <tt>false</tt> otherwise. |
403 |
> |
* |
404 |
> |
* @return {@code true} if one or more parties broke out of this |
405 |
> |
* barrier due to interruption or timeout since |
406 |
> |
* construction or the last reset, or a barrier action |
407 |
> |
* failed due to an exception; {@code false} otherwise. |
408 |
|
*/ |
409 |
|
public boolean isBroken() { |
410 |
|
final ReentrantLock lock = this.lock; |
440 |
|
* Returns the number of parties currently waiting at the barrier. |
441 |
|
* This method is primarily useful for debugging and assertions. |
442 |
|
* |
443 |
< |
* @return the number of parties currently blocked in {@link #await}. |
443 |
> |
* @return the number of parties currently blocked in {@link #await} |
444 |
|
*/ |
445 |
|
public int getNumberWaiting() { |
446 |
|
final ReentrantLock lock = this.lock; |