124 |
|
* The typical idiom is for the method setting this up to first |
125 |
|
* register, then start the actions, then deregister, as in: |
126 |
|
* |
127 |
< |
* <pre> {@code |
127 |
> |
* <pre> {@code |
128 |
|
* void runTasks(List<Runnable> tasks) { |
129 |
|
* final Phaser phaser = new Phaser(1); // "1" to register self |
130 |
|
* // create and start threads |
145 |
|
* <p>One way to cause a set of threads to repeatedly perform actions |
146 |
|
* for a given number of iterations is to override {@code onAdvance}: |
147 |
|
* |
148 |
< |
* <pre> {@code |
148 |
> |
* <pre> {@code |
149 |
|
* void startTasks(List<Runnable> tasks, final int iterations) { |
150 |
|
* final Phaser phaser = new Phaser() { |
151 |
|
* protected boolean onAdvance(int phase, int registeredParties) { |
169 |
|
* |
170 |
|
* If the main task must later await termination, it |
171 |
|
* may re-register and then execute a similar loop: |
172 |
< |
* <pre> {@code |
172 |
> |
* <pre> {@code |
173 |
|
* // ... |
174 |
|
* phaser.register(); |
175 |
|
* while (!phaser.isTerminated()) |
179 |
|
* in contexts where you are sure that the phase will never wrap around |
180 |
|
* {@code Integer.MAX_VALUE}. For example: |
181 |
|
* |
182 |
< |
* <pre> {@code |
182 |
> |
* <pre> {@code |
183 |
|
* void awaitPhase(Phaser phaser, int phase) { |
184 |
|
* int p = phaser.register(); // assumes caller not already registered |
185 |
|
* while (p < phase) { |
199 |
|
* new Phaser())}, these tasks could then be started, for example by |
200 |
|
* submitting to a pool: |
201 |
|
* |
202 |
< |
* <pre> {@code |
202 |
> |
* <pre> {@code |
203 |
|
* void build(Task[] tasks, int lo, int hi, Phaser ph) { |
204 |
|
* if (hi - lo > TASKS_PER_PHASER) { |
205 |
|
* for (int i = lo; i < hi; i += TASKS_PER_PHASER) { |