--- jsr166/src/jsr166y/Phaser.java 2009/08/23 20:12:24 1.36 +++ jsr166/src/jsr166y/Phaser.java 2009/08/24 12:49:39 1.40 @@ -12,93 +12,100 @@ import java.util.concurrent.atomic.Atomi import java.util.concurrent.locks.LockSupport; /** - * A reusable synchronization barrier, similar in functionality to a + * A reusable synchronization barrier, similar in functionality to * {@link java.util.concurrent.CyclicBarrier CyclicBarrier} and * {@link java.util.concurrent.CountDownLatch CountDownLatch} * but supporting more flexible usage. * - * + * #getPhase}). When the remaining ({@link #getUnarrivedParties}) + * parties arrive, the phase advances; thus, this value is always + * greater than zero if there are any registered parties. The values + * returned by these methods may reflect transient states and so are + * not in general useful for synchronization control. Method {@link + * #toString} returns snapshots of these state queries in a form + * convenient for informal monitoring. * *

Sample usages: * @@ -131,7 +138,7 @@ import java.util.concurrent.locks.LockSu *

 {@code
  * void startTasks(List tasks, final int iterations) {
  *   final Phaser phaser = new Phaser() {
- *     public boolean onAdvance(int phase, int registeredParties) {
+ *     protected boolean onAdvance(int phase, int registeredParties) {
  *       return phase >= iterations || registeredParties == 0;
  *     }
  *   };
@@ -150,6 +157,33 @@ import java.util.concurrent.locks.LockSu
  *   phaser.arriveAndDeregister(); // deregister self, don't wait
  * }}
* + * If the main task must later await termination, it + * may re-register and then execute a similar loop: + *
 {@code
+ *   // ...
+ *   phaser.register();
+ *   while (!phaser.isTerminated())
+ *     phaser.arriveAndAwaitAdvance();
+ * }
+ * + * Related constructions may be used to await particular phase numbers + * in contexts where you are sure that the phase will never wrap around + * {@code Integer.MAX_VALUE}. For example: + * + *
 {@code
+ *   void awaitPhase(Phaser phaser, int phase) {
+ *     int p = phaser.register(); // assumes caller not already registered
+ *     while (p < phase) {
+ *       if (phaser.isTerminated())
+ *         // ... deal with unexpected termination
+ *       else
+ *         p = phaser.arriveAndAwaitAdvance();
+ *     }
+ *     phaser.arriveAndDeregister();
+ *   }
+ * }
+ * + * *

To create a set of tasks using a tree of phasers, * you could use code of the following form, assuming a * Task class with a constructor accepting a phaser that @@ -426,7 +460,9 @@ public class Phaser { /** * Arrives at the barrier, but does not wait for others. (You can - * in turn wait for others via {@link #awaitAdvance}). + * in turn wait for others via {@link #awaitAdvance}). It is an + * unenforced usage error for an unregistered party to invoke this + * method. * * @return the arrival phase number, or a negative value if terminated * @throws IllegalStateException if not terminated and the number @@ -478,7 +514,8 @@ public class Phaser { * required to trip the barrier in future phases. If this phaser * has a parent, and deregistration causes this phaser to have * zero parties, this phaser also arrives at and is deregistered - * from its parent. + * from its parent. It is an unenforced usage error for an + * unregistered party to invoke this method. * * @return the arrival phase number, or a negative value if terminated * @throws IllegalStateException if not terminated and the number @@ -534,7 +571,8 @@ public class Phaser { * interruption or timeout, you can arrange this with an analogous * construction using one of the other forms of the awaitAdvance * method. If instead you need to deregister upon arrival use - * {@code arriveAndDeregister}. + * {@code arriveAndDeregister}. It is an unenforced usage error + * for an unregistered party to invoke this method. * * @return the arrival phase number, or a negative number if terminated * @throws IllegalStateException if not terminated and the number @@ -548,7 +586,8 @@ public class Phaser { * Awaits the phase of the barrier to advance from the given phase * value, returning immediately if the current phase of the * barrier is not equal to the given phase value or this barrier - * is terminated. + * is terminated. It is an unenforced usage error for an + * unregistered party to invoke this method. * * @param phase an arrival phase number, or negative value if * terminated; this argument is normally the value returned by a @@ -571,10 +610,11 @@ public class Phaser { /** * Awaits the phase of the barrier to advance from the given phase - * value, throwing {@code InterruptedException} if interrupted while - * waiting, or returning immediately if the current phase of the - * barrier is not equal to the given phase value or this barrier - * is terminated. + * value, throwing {@code InterruptedException} if interrupted + * while waiting, or returning immediately if the current phase of + * the barrier is not equal to the given phase value or this + * barrier is terminated. It is an unenforced usage error for an + * unregistered party to invoke this method. * * @param phase an arrival phase number, or negative value if * terminated; this argument is normally the value returned by a @@ -598,10 +638,12 @@ public class Phaser { /** * Awaits the phase of the barrier to advance from the given phase - * value or the given timeout to elapse, throwing - * {@code InterruptedException} if interrupted while waiting, or - * returning immediately if the current phase of the barrier is not - * equal to the given phase value or this barrier is terminated. + * value or the given timeout to elapse, throwing {@code + * InterruptedException} if interrupted while waiting, or + * returning immediately if the current phase of the barrier is + * not equal to the given phase value or this barrier is + * terminated. It is an unenforced usage error for an + * unregistered party to invoke this method. * * @param phase an arrival phase number, or negative value if * terminated; this argument is normally the value returned by a