ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/main/java/util/concurrent/Phaser.java
(Generate patch)

Comparing jsr166/src/main/java/util/concurrent/Phaser.java (file contents):
Revision 1.6 by jsr166, Wed Aug 5 01:52:34 2009 UTC vs.
Revision 1.7 by jsr166, Wed Aug 12 04:10:59 2009 UTC

# Line 34 | Line 34 | import java.util.concurrent.locks.LockSu
34   * awaited.  Method {@link #arriveAndAwaitAdvance} has effect
35   * analogous to {@link java.util.concurrent.CyclicBarrier#await
36   * CyclicBarrier.await}.  However, phasers separate two aspects of
37 < * coordination, that may also be invoked independently:
37 > * coordination, which may also be invoked independently:
38   *
39   * <ul>
40   *
# Line 49 | Line 49 | import java.util.concurrent.locks.LockSu
49   *
50   *
51   * <li> Barrier actions, performed by the task triggering a phase
52 < * advance while others may be waiting, are arranged by overriding
53 < * method {@link #onAdvance}, that also controls termination.
54 < * Overriding this method may be used to similar but more flexible
55 < * effect as providing a barrier action to a {@code CyclicBarrier}.
52 > * advance, are arranged by overriding method {@link #onAdvance(int,
53 > * int)}, which also controls termination. Overriding this method is
54 > * similar to, but more flexible than, providing a barrier action to a
55 > * {@code CyclicBarrier}.
56   *
57   * <li> Phasers may enter a <em>termination</em> state in which all
58   * actions immediately return without updating phaser state or waiting
59   * for advance, and indicating (via a negative phase value) that
60 < * execution is complete.  Termination is triggered by executing the
61 < * overridable {@code onAdvance} method that is invoked each time the
62 < * barrier is about to be tripped. When a phaser is controlling an
63 < * action with a fixed number of iterations, it is often convenient to
64 < * override this method to cause termination when the current phase
65 < * number reaches a threshold. Method {@link #forceTermination} is also
66 < * available to abruptly release waiting threads and allow them to
67 < * terminate.
60 > * execution is complete.  Termination is triggered when an invocation
61 > * of {@code onAdvance} returns {@code true}.  When a phaser is
62 > * controlling an action with a fixed number of iterations, it is
63 > * often convenient to override this method to cause termination when
64 > * the current phase number reaches a threshold. Method {@link
65 > * #forceTermination} is also available to abruptly release waiting
66 > * threads and allow them to terminate.
67   *
68   * <li> Phasers may be tiered to reduce contention. Phasers with large
69   * numbers of parties that would otherwise experience heavy
# Line 80 | Line 79 | import java.util.concurrent.locks.LockSu
79   * within handlers of those exceptions, often after invoking
80   * {@code forceTermination}.
81   *
82 < * <li>Phasers ensure lack of starvation when used by ForkJoinTasks.
82 > * <li>Phasers may be used to coordinate tasks executing in a {@link
83 > * ForkJoinPool}, which will ensure sufficient parallelism to execute
84 > * tasks when others are blocked waiting for a phase to advance.
85   *
86   * </ul>
87   *
# Line 94 | Line 95 | import java.util.concurrent.locks.LockSu
95   *  <pre> {@code
96   * void runTasks(List<Runnable> list) {
97   *   final Phaser phaser = new Phaser(1); // "1" to register self
98 + *   // create and start threads
99   *   for (Runnable r : list) {
100   *     phaser.register();
101   *     new Thread() {
102   *       public void run() {
103   *         phaser.arriveAndAwaitAdvance(); // await all creation
104   *         r.run();
103 *         phaser.arriveAndDeregister();   // signal completion
105   *       }
106   *     }.start();
107   *   }
108   *
109 < *   doSomethingOnBehalfOfWorkers();
110 < *   phaser.arrive(); // allow threads to start
110 < *   int p = phaser.arriveAndDeregister(); // deregister self  ...
111 < *   p = phaser.awaitAdvance(p); // ... and await arrival
112 < *   otherActions(); // do other things while tasks execute
113 < *   phaser.awaitAdvance(p); // await final completion
109 > *   // allow threads to start and deregister self
110 > *   phaser.arriveAndDeregister();
111   * }}</pre>
112   *
113   * <p>One way to cause a set of threads to repeatedly perform actions
# Line 462 | Line 459 | public class Phaser {
459      }
460  
461      /**
462 <     * Arrives at the barrier, and deregisters from it, without
463 <     * waiting for others. Deregistration reduces number of parties
462 >     * Arrives at the barrier and deregisters from it without waiting
463 >     * for others. Deregistration reduces the number of parties
464       * required to trip the barrier in future phases.  If this phaser
465       * has a parent, and deregistration causes this phaser to have
466 <     * zero parties, this phaser is also deregistered from its parent.
466 >     * zero parties, this phaser also arrives at and is deregistered
467 >     * from its parent.
468       *
469       * @return the current barrier phase number upon entry to
470       * this method, or a negative value if terminated
# Line 519 | Line 517 | public class Phaser {
517  
518      /**
519       * Arrives at the barrier and awaits others. Equivalent in effect
520 <     * to {@code awaitAdvance(arrive())}.  If you instead need to
521 <     * await with interruption of timeout, and/or deregister upon
522 <     * arrival, you can arrange them using analogous constructions.
520 >     * to {@code awaitAdvance(arrive())}.  If you need to await with
521 >     * interruption or timeout, you can arrange this with an analogous
522 >     * construction using one of the other forms of the awaitAdvance
523 >     * method.  If instead you need to deregister upon arrival use
524 >     * {@code arriveAndDeregister}.
525       *
526       * @return the phase on entry to this method
527       * @throws IllegalStateException if not terminated and the number
# Line 532 | Line 532 | public class Phaser {
532      }
533  
534      /**
535 <     * Awaits the phase of the barrier to advance from the given
536 <     * value, or returns immediately if argument is negative or this
537 <     * barrier is terminated.
535 >     * Awaits the phase of the barrier to advance from the given phase
536 >     * value, or returns immediately if the current phase of the barrier
537 >     * is not equal to the given phase value or this barrier is
538 >     * terminated.
539       *
540       * @param phase the phase on entry to this method
541       * @return the phase on exit from this method
# Line 635 | Line 636 | public class Phaser {
636      }
637  
638      /**
638     * Returns {@code true} if the current phase number equals the given phase.
639     *
640     * @param phase the phase
641     * @return {@code true} if the current phase number equals the given phase
642     */
643    public final boolean hasPhase(int phase) {
644        return phaseOf(getReconciledState()) == phase;
645    }
646
647    /**
639       * Returns the number of parties registered at this barrier.
640       *
641       * @return the number of parties
# Line 719 | Line 710 | public class Phaser {
710       * effects visible to participating tasks, but it is in general
711       * only sensible to do so in designs where all parties register
712       * before any arrive, and all {@link #awaitAdvance} at each phase.
713 <     * Otherwise, you cannot ensure lack of interference. In
714 <     * particular, this method may be invoked more than once per
724 <     * transition if other parties successfully register while the
725 <     * invocation of this method is in progress, thus postponing the
726 <     * transition until those parties also arrive, re-triggering this
727 <     * method.
713 >     * Otherwise, you cannot ensure lack of interference from other
714 >     * parties during the invocation of this method.
715       *
716       * @param phase the phase number on entering the barrier
717       * @param registeredParties the current number of registered parties

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines