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

Comparing jsr166/src/jsr166y/Phaser.java (file contents):
Revision 1.40 by dl, Mon Aug 24 12:49:39 2009 UTC vs.
Revision 1.44 by dl, Tue Aug 25 16:32:28 2009 UTC

# Line 100 | Line 100 | import java.util.concurrent.locks.LockSu
100   * #getRegisteredParties} parties in total, of which {@link
101   * #getArrivedParties} have arrived at the current phase ({@link
102   * #getPhase}).  When the remaining ({@link #getUnarrivedParties})
103 < * parties arrive, the phase advances; thus, this value is always
104 < * greater than zero if there are any registered parties.  The values
105 < * returned by these methods may reflect transient states and so are
106 < * not in general useful for synchronization control.  Method {@link
107 < * #toString} returns snapshots of these state queries in a form
108 < * convenient for informal monitoring.
103 > * parties arrive, the phase advances.  The values returned by these
104 > * methods may reflect transient states and so are not in general
105 > * useful for synchronization control.  Method {@link #toString}
106 > * returns snapshots of these state queries in a form convenient for
107 > * informal monitoring.
108   *
109   * <p><b>Sample usages:</b>
110   *
# Line 189 | Line 188 | import java.util.concurrent.locks.LockSu
188   * Task class with a constructor accepting a phaser that
189   * it registers for upon construction:
190   *  <pre> {@code
191 < * void build(Task[] actions, int lo, int hi, Phaser b) {
192 < *   int step = (hi - lo) / TASKS_PER_PHASER;
193 < *   if (step > 1) {
194 < *     int i = lo;
195 < *     while (i < hi) {
197 < *       int r = Math.min(i + step, hi);
198 < *       build(actions, i, r, new Phaser(b));
199 < *       i = r;
191 > * void build(Task[] actions, int lo, int hi, Phaser ph) {
192 > *   if (hi - lo > TASKS_PER_PHASER) {
193 > *     for (int i = lo; i < hi; i += TASKS_PER_PHASER) {
194 > *       int j = Math.min(i + TASKS_PER_PHASER, hi);
195 > *       build(actions, i, j, new Phaser(ph));
196   *     }
197   *   } else {
198   *     for (int i = lo; i < hi; ++i)
199 < *       actions[i] = new Task(b);
200 < *       // assumes new Task(b) performs b.register()
199 > *       actions[i] = new Task(ph);
200 > *       // assumes new Task(ph) performs ph.register()
201   *   }
202   * }
203   * // .. initially called, for n tasks via
# Line 250 | Line 246 | public class Phaser {
246       */
247      private volatile long state;
248  
253    private static final int ushortBits = 16;
249      private static final int ushortMask = 0xffff;
250      private static final int phaseMask  = 0x7fffffff;
251  
# Line 764 | Line 759 | public class Phaser {
759      }
760  
761      /**
762 <     * Overridable method to perform an action upon phase advance, and
763 <     * to control termination. This method is invoked whenever the
764 <     * barrier is tripped (and thus all other waiting parties are
765 <     * dormant). If it returns {@code true}, then, rather than advance
766 <     * the phase number, this barrier will be set to a final
767 <     * termination state, and subsequent calls to {@link #isTerminated}
768 <     * will return true.
762 >     * Overridable method to perform an action upon impending phase
763 >     * advance, and to control termination. This method is invoked
764 >     * upon arrival of the party tripping the barrier (when all other
765 >     * waiting parties are dormant).  If this method returns {@code
766 >     * true}, then, rather than advance the phase number, this barrier
767 >     * will be set to a final termination state, and subsequent calls
768 >     * to {@link #isTerminated} will return true. Any (unchecked)
769 >     * Exception or Error thrown by an invocation of this method is
770 >     * propagated to the party attempting to trip the barrier, in
771 >     * which case no advance occurs.
772 >     *
773 >     * <p>The arguments to this method provide the state of the phaser
774 >     * prevailing for the current transition. (When called from within
775 >     * an implementation of {@code onAdvance} the values returned by
776 >     * methods such as {@code getPhase} may or may not reliably
777 >     * indicate the state to which this transition applies.)
778       *
779       * <p>The default version returns {@code true} when the number of
780       * registered parties is zero. Normally, overrides that arrange
# Line 778 | Line 782 | public class Phaser {
782       * property.
783       *
784       * <p>You may override this method to perform an action with side
785 <     * effects visible to participating tasks, but it is in general
786 <     * only sensible to do so in designs where all parties register
787 <     * before any arrive, and all {@link #awaitAdvance} at each phase.
788 <     * Otherwise, you cannot ensure lack of interference from other
789 <     * parties during the invocation of this method.
785 >     * effects visible to participating tasks, but doing so requires
786 >     * care: Method {@code onAdvance} may be invoked more than once
787 >     * per transition.  Further, unless all parties register before
788 >     * any arrive, and all {@link #awaitAdvance} at each phase, then
789 >     * you cannot ensure lack of interference from other parties
790 >     * during the invocation of this method.
791       *
792       * @param phase the phase number on entering the barrier
793       * @param registeredParties the current number of registered parties

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines