--- jsr166/src/jsr166y/Phaser.java 2009/08/19 15:23:18 1.30 +++ jsr166/src/jsr166y/Phaser.java 2009/08/24 00:48:52 1.37 @@ -12,25 +12,29 @@ 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. * * * - * *
  • Barrier actions, performed by the task triggering a phase * advance, are arranged by overriding method {@link #onAdvance(int, * int)}, which also controls termination. Overriding this method is @@ -85,6 +90,14 @@ import java.util.concurrent.locks.LockSu * ForkJoinPool}, which will ensure sufficient parallelism to execute * tasks when others are blocked waiting for a phase to advance. * + *
  • The current state of a phaser may be monitored. At any given + * moment there are {@link #getRegisteredParties}, where {@link + * #getArrivedParties} have arrived at the current phase ({@link + * #getPhase}). When the remaining {@link #getUnarrivedParties}) + * arrive, the phase advances. Method {@link #toString} returns + * snapshots of these state queries in a form convenient for + * informal monitoring. + * * * *

    Sample usages: @@ -95,15 +108,15 @@ import java.util.concurrent.locks.LockSu * first register, then start the actions, then deregister, as in: * *

     {@code
    - * void runTasks(List list) {
    + * void runTasks(List tasks) {
      *   final Phaser phaser = new Phaser(1); // "1" to register self
      *   // create and start threads
    - *   for (Runnable r : list) {
    + *   for (Runnable task : tasks) {
      *     phaser.register();
      *     new Thread() {
      *       public void run() {
      *         phaser.arriveAndAwaitAdvance(); // await all creation
    - *         r.run();
    + *         task.run();
      *       }
      *     }.start();
      *   }
    @@ -116,19 +129,19 @@ import java.util.concurrent.locks.LockSu
      * for a given number of iterations is to override {@code onAdvance}:
      *
      *  
     {@code
    - * void startTasks(List list, final int iterations) {
    + * void startTasks(List tasks, final int iterations) {
      *   final Phaser phaser = new Phaser() {
      *     public boolean onAdvance(int phase, int registeredParties) {
      *       return phase >= iterations || registeredParties == 0;
      *     }
      *   };
      *   phaser.register();
    - *   for (Runnable r : list) {
    + *   for (Runnable task : tasks) {
      *     phaser.register();
      *     new Thread() {
      *       public void run() {
      *         do {
    - *           r.run();
    + *           task.run();
      *           phaser.arriveAndAwaitAdvance();
      *         } while(!phaser.isTerminated();
      *       }
    @@ -169,7 +182,7 @@ import java.util.concurrent.locks.LockSu
      *
      * 

    Implementation notes: This implementation restricts the * maximum number of parties to 65535. Attempts to register additional - * parties result in IllegalStateExceptions. However, you can and + * parties result in {@code IllegalStateException}. However, you can and * should create tiered phasers to accommodate arbitrarily large sets * of participants. * @@ -366,7 +379,7 @@ public class Phaser { /** * Adds a new unarrived party to this phaser. * - * @return the current barrier phase number upon registration + * @return the arrival phase number to which this registration applied * @throws IllegalStateException if attempting to register more * than the maximum supported number of parties */ @@ -378,7 +391,7 @@ public class Phaser { * Adds the given number of new unarrived parties to this phaser. * * @param parties the number of parties required to trip barrier - * @return the current barrier phase number upon registration + * @return the arrival phase number to which this registration applied * @throws IllegalStateException if attempting to register more * than the maximum supported number of parties */ @@ -415,8 +428,7 @@ public class Phaser { * Arrives at the barrier, but does not wait for others. (You can * in turn wait for others via {@link #awaitAdvance}). * - * @return the barrier phase number upon entry to this method, or a - * negative value if terminated + * @return the arrival phase number, or a negative value if terminated * @throws IllegalStateException if not terminated and the number * of unarrived parties would become negative */ @@ -468,8 +480,7 @@ public class Phaser { * zero parties, this phaser also arrives at and is deregistered * from its parent. * - * @return the current barrier phase number upon entry to - * this method, or a negative value if terminated + * @return the arrival phase number, or a negative value if terminated * @throws IllegalStateException if not terminated and the number * of registered or unarrived parties would become negative */ @@ -525,7 +536,7 @@ public class Phaser { * method. If instead you need to deregister upon arrival use * {@code arriveAndDeregister}. * - * @return the phase on entry to this method + * @return the arrival phase number, or a negative number if terminated * @throws IllegalStateException if not terminated and the number * of unarrived parties would become negative */ @@ -539,8 +550,11 @@ public class Phaser { * barrier is not equal to the given phase value or this barrier * is terminated. * - * @param phase the phase on entry to this method - * @return the phase on exit from this method + * @param phase an arrival phase number, or negative value if + * terminated; this argument is normally the value returned by a + * previous call to {@code arrive} or its variants + * @return the next arrival phase number, or a negative value + * if terminated or argument is negative */ public int awaitAdvance(int phase) { if (phase < 0) @@ -557,13 +571,16 @@ public class Phaser { /** * Awaits the phase of the barrier to advance from the given phase - * value, throwing InterruptedException if interrupted while + * 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 + * is terminated. * - * @param phase the phase on entry to this method - * @return the phase on exit from this method + * @param phase an arrival phase number, or negative value if + * terminated; this argument is normally the value returned by a + * previous call to {@code arrive} or its variants + * @return the next arrival phase number, or a negative value + * if terminated or argument is negative * @throws InterruptedException if thread interrupted while waiting */ public int awaitAdvanceInterruptibly(int phase) @@ -581,13 +598,20 @@ public class Phaser { /** * Awaits the phase of the barrier to advance from the given phase - * value or the given timeout elapses, throwing - * 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. - * - * @param phase the phase on entry to this method - * @return the phase on exit from this method + * 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. + * + * @param phase an arrival phase number, or negative value if + * terminated; this argument is normally the value returned by a + * previous call to {@code arrive} or its variants + * @param timeout how long to wait before giving up, in units of + * {@code unit} + * @param unit a {@code TimeUnit} determining how to interpret the + * {@code timeout} parameter + * @return the next arrival phase number, or a negative value + * if terminated or argument is negative * @throws InterruptedException if thread interrupted while waiting * @throws TimeoutException if timed out while waiting */ @@ -650,8 +674,8 @@ public class Phaser { } /** - * Returns the number of parties that have arrived at the current - * phase of this barrier. + * Returns the number of registered parties that have arrived at + * the current phase of this barrier. * * @return the number of arrived parties */