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 |
781 |
|
* property. |
782 |
|
* |
783 |
|
* <p>You may override this method to perform an action with side |
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. |
784 |
> |
* effects visible to participating tasks, but it is only sensible |
785 |
> |
* to do so in designs where all parties register before any |
786 |
> |
* 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. Additionally, |
789 |
> |
* method {@code onAdvance} may be invoked more than once per |
790 |
> |
* transition if registrations are intermixed with arrivals. |
791 |
|
* |
792 |
|
* @param phase the phase number on entering the barrier |
793 |
|
* @param registeredParties the current number of registered parties |
871 |
|
boolean doWait() { |
872 |
|
if (thread != null) { |
873 |
|
try { |
874 |
< |
ForkJoinPool.managedBlock(this, false); |
874 |
> |
ForkJoinPool.managedBlock(this); |
875 |
|
} catch (InterruptedException ie) { |
876 |
|
} |
877 |
|
} |