142 |
|
* } |
143 |
|
* }; |
144 |
|
* phaser.register(); |
145 |
< |
* for (Runnable task : tasks) { |
145 |
> |
* for (final Runnable task : tasks) { |
146 |
|
* phaser.register(); |
147 |
|
* new Thread() { |
148 |
|
* public void run() { |
149 |
|
* do { |
150 |
|
* task.run(); |
151 |
|
* phaser.arriveAndAwaitAdvance(); |
152 |
< |
* } while(!phaser.isTerminated(); |
152 |
> |
* } while (!phaser.isTerminated()); |
153 |
|
* } |
154 |
|
* }.start(); |
155 |
|
* } |
158 |
|
* |
159 |
|
* If the main task must later await termination, it |
160 |
|
* may re-register and then execute a similar loop: |
161 |
< |
* <pre> {@code |
161 |
> |
* <pre> {@code |
162 |
|
* // ... |
163 |
|
* phaser.register(); |
164 |
|
* while (!phaser.isTerminated()) |
165 |
< |
* phaser.arriveAndAwaitAdvance(); |
166 |
< |
* }</pre> |
165 |
> |
* phaser.arriveAndAwaitAdvance();}</pre> |
166 |
|
* |
167 |
< |
* Related constructions may be used to await particular phase numbers |
167 |
> |
* <p>Related constructions may be used to await particular phase numbers |
168 |
|
* in contexts where you are sure that the phase will never wrap around |
169 |
|
* {@code Integer.MAX_VALUE}. For example: |
170 |
|
* |
171 |
< |
* <pre> {@code |
172 |
< |
* void awaitPhase(Phaser phaser, int phase) { |
173 |
< |
* int p = phaser.register(); // assumes caller not already registered |
174 |
< |
* while (p < phase) { |
175 |
< |
* if (phaser.isTerminated()) |
176 |
< |
* // ... deal with unexpected termination |
177 |
< |
* else |
178 |
< |
* p = phaser.arriveAndAwaitAdvance(); |
180 |
< |
* } |
181 |
< |
* phaser.arriveAndDeregister(); |
171 |
> |
* <pre> {@code |
172 |
> |
* void awaitPhase(Phaser phaser, int phase) { |
173 |
> |
* int p = phaser.register(); // assumes caller not already registered |
174 |
> |
* while (p < phase) { |
175 |
> |
* if (phaser.isTerminated()) |
176 |
> |
* // ... deal with unexpected termination |
177 |
> |
* else |
178 |
> |
* p = phaser.arriveAndAwaitAdvance(); |
179 |
|
* } |
180 |
< |
* }</pre> |
180 |
> |
* phaser.arriveAndDeregister(); |
181 |
> |
* }}</pre> |
182 |
|
* |
183 |
|
* |
184 |
|
* <p>To create a set of tasks using a tree of phasers, |
185 |
|
* you could use code of the following form, assuming a |
186 |
|
* Task class with a constructor accepting a phaser that |
187 |
|
* it registers for upon construction: |
188 |
+ |
* |
189 |
|
* <pre> {@code |
190 |
< |
* void build(Task[] actions, int lo, int hi, Phaser b) { |
191 |
< |
* int step = (hi - lo) / TASKS_PER_PHASER; |
192 |
< |
* if (step > 1) { |
193 |
< |
* int i = lo; |
194 |
< |
* while (i < hi) { |
196 |
< |
* int r = Math.min(i + step, hi); |
197 |
< |
* build(actions, i, r, new Phaser(b)); |
198 |
< |
* i = r; |
190 |
> |
* void build(Task[] actions, int lo, int hi, Phaser ph) { |
191 |
> |
* if (hi - lo > TASKS_PER_PHASER) { |
192 |
> |
* for (int i = lo; i < hi; i += TASKS_PER_PHASER) { |
193 |
> |
* int j = Math.min(i + TASKS_PER_PHASER, hi); |
194 |
> |
* build(actions, i, j, new Phaser(ph)); |
195 |
|
* } |
196 |
|
* } else { |
197 |
|
* for (int i = lo; i < hi; ++i) |
198 |
< |
* actions[i] = new Task(b); |
199 |
< |
* // assumes new Task(b) performs b.register() |
198 |
> |
* actions[i] = new Task(ph); |
199 |
> |
* // assumes new Task(ph) performs ph.register() |
200 |
|
* } |
201 |
|
* } |
202 |
|
* // .. initially called, for n tasks via |
758 |
|
} |
759 |
|
|
760 |
|
/** |
761 |
< |
* Overridable method to perform an action upon phase advance, and |
762 |
< |
* to control termination. This method is invoked upon arrival of |
763 |
< |
* the party tripping the barrier (when all other waiting parties |
764 |
< |
* are dormant). If this method returns {@code true}, then, |
765 |
< |
* rather than advance the phase number, this barrier will be set |
766 |
< |
* to a final termination state, and subsequent calls to {@link |
767 |
< |
* #isTerminated} will return true. Any (unchecked) Exception or |
768 |
< |
* Error thrown by an invocation of this method is propagated to |
769 |
< |
* the party attempting to trip the barrier, in which case no |
770 |
< |
* advance occurs. |
761 |
> |
* Overridable method to perform an action upon impending phase |
762 |
> |
* advance, and to control termination. This method is invoked |
763 |
> |
* upon arrival of the party tripping the barrier (when all other |
764 |
> |
* waiting parties are dormant). If this method returns {@code |
765 |
> |
* true}, then, rather than advance the phase number, this barrier |
766 |
> |
* will be set to a final termination state, and subsequent calls |
767 |
> |
* to {@link #isTerminated} will return true. Any (unchecked) |
768 |
> |
* Exception or Error thrown by an invocation of this method is |
769 |
> |
* propagated to the party attempting to trip the barrier, in |
770 |
> |
* which case no advance occurs. |
771 |
|
* |
772 |
|
* <p>The arguments to this method provide the state of the phaser |
773 |
|
* prevailing for the current transition. (When called from within |
781 |
|
* property. |
782 |
|
* |
783 |
|
* <p>You may override this method to perform an action with side |
784 |
< |
* effects visible to participating tasks, but it is in general |
785 |
< |
* only sensible to do so in designs where all parties register |
786 |
< |
* before any arrive, and all {@link #awaitAdvance} at each phase. |
787 |
< |
* Otherwise, you cannot ensure lack of interference from other |
788 |
< |
* parties during the invocation of this method. |
784 |
> |
* effects visible to participating tasks, but doing so requires |
785 |
> |
* care: Method {@code onAdvance} may be invoked more than once |
786 |
> |
* per transition. Further, unless all parties register before |
787 |
> |
* any arrive, and all {@link #awaitAdvance} at each phase, then |
788 |
> |
* you cannot ensure lack of interference from other parties |
789 |
> |
* during the invocation of this method. |
790 |
|
* |
791 |
|
* @param phase the phase number on entering the barrier |
792 |
|
* @param registeredParties the current number of registered parties |