95 |
|
* first register, then start the actions, then deregister, as in: |
96 |
|
* |
97 |
|
* <pre> {@code |
98 |
< |
* void runTasks(List<Runnable> list) { |
98 |
> |
* void runTasks(List<Runnable> tasks) { |
99 |
|
* final Phaser phaser = new Phaser(1); // "1" to register self |
100 |
|
* // create and start threads |
101 |
< |
* for (Runnable r : list) { |
101 |
> |
* for (Runnable task : tasks) { |
102 |
|
* phaser.register(); |
103 |
|
* new Thread() { |
104 |
|
* public void run() { |
105 |
|
* phaser.arriveAndAwaitAdvance(); // await all creation |
106 |
< |
* r.run(); |
106 |
> |
* task.run(); |
107 |
|
* } |
108 |
|
* }.start(); |
109 |
|
* } |
116 |
|
* for a given number of iterations is to override {@code onAdvance}: |
117 |
|
* |
118 |
|
* <pre> {@code |
119 |
< |
* void startTasks(List<Runnable> list, final int iterations) { |
119 |
> |
* void startTasks(List<Runnable> tasks, final int iterations) { |
120 |
|
* final Phaser phaser = new Phaser() { |
121 |
|
* public boolean onAdvance(int phase, int registeredParties) { |
122 |
|
* return phase >= iterations || registeredParties == 0; |
123 |
|
* } |
124 |
|
* }; |
125 |
|
* phaser.register(); |
126 |
< |
* for (Runnable r : list) { |
126 |
> |
* for (Runnable task : tasks) { |
127 |
|
* phaser.register(); |
128 |
|
* new Thread() { |
129 |
|
* public void run() { |
130 |
|
* do { |
131 |
< |
* r.run(); |
131 |
> |
* task.run(); |
132 |
|
* phaser.arriveAndAwaitAdvance(); |
133 |
|
* } while(!phaser.isTerminated(); |
134 |
|
* } |
169 |
|
* |
170 |
|
* <p><b>Implementation notes</b>: This implementation restricts the |
171 |
|
* maximum number of parties to 65535. Attempts to register additional |
172 |
< |
* parties result in IllegalStateExceptions. However, you can and |
172 |
> |
* parties result in {@code IllegalStateException}. However, you can and |
173 |
|
* should create tiered phasers to accommodate arbitrarily large sets |
174 |
|
* of participants. |
175 |
|
* |
540 |
|
* is terminated. |
541 |
|
* |
542 |
|
* @param phase the phase on entry to this method |
543 |
< |
* @return the phase on exit from this method |
543 |
> |
* @return the current barrier phase number upon exit of |
544 |
> |
* this method, or a negative value if terminated or |
545 |
> |
* argument is negative |
546 |
|
*/ |
547 |
|
public int awaitAdvance(int phase) { |
548 |
|
if (phase < 0) |
559 |
|
|
560 |
|
/** |
561 |
|
* Awaits the phase of the barrier to advance from the given phase |
562 |
< |
* value, throwing InterruptedException if interrupted while |
562 |
> |
* value, throwing {@code InterruptedException} if interrupted while |
563 |
|
* waiting, or returning immediately if the current phase of the |
564 |
|
* barrier is not equal to the given phase value or this barrier |
565 |
< |
* is terminated |
565 |
> |
* is terminated. |
566 |
|
* |
567 |
|
* @param phase the phase on entry to this method |
568 |
< |
* @return the phase on exit from this method |
568 |
> |
* @return the current barrier phase number upon exit of |
569 |
> |
* this method, or a negative value if terminated or |
570 |
> |
* argument is negative |
571 |
|
* @throws InterruptedException if thread interrupted while waiting |
572 |
|
*/ |
573 |
|
public int awaitAdvanceInterruptibly(int phase) |
585 |
|
|
586 |
|
/** |
587 |
|
* Awaits the phase of the barrier to advance from the given phase |
588 |
< |
* value or the given timeout elapses, throwing |
589 |
< |
* InterruptedException if interrupted while waiting, or returning |
590 |
< |
* immediately if the current phase of the barrier is not equal to |
591 |
< |
* the given phase value or this barrier is terminated. |
588 |
> |
* value or the given timeout to elapse, throwing |
589 |
> |
* {@code InterruptedException} if interrupted while waiting, or |
590 |
> |
* returning immediately if the current phase of the barrier is not |
591 |
> |
* equal to the given phase value or this barrier is terminated. |
592 |
|
* |
593 |
|
* @param phase the phase on entry to this method |
594 |
|
* @param timeout how long to wait before giving up, in units of |
595 |
|
* {@code unit} |
596 |
|
* @param unit a {@code TimeUnit} determining how to interpret the |
597 |
|
* {@code timeout} parameter |
598 |
< |
* @return the phase on exit from this method |
598 |
> |
* @return the current barrier phase number upon exit of |
599 |
> |
* this method, or a negative value if terminated or |
600 |
> |
* argument is negative |
601 |
|
* @throws InterruptedException if thread interrupted while waiting |
602 |
|
* @throws TimeoutException if timed out while waiting |
603 |
|
*/ |