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 ph) { |
191 |
|
* if (hi - lo > TASKS_PER_PHASER) { |