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.46 by dl, Sun Aug 30 11:08:25 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 143 | Line 142 | import java.util.concurrent.locks.LockSu
142   *     }
143   *   };
144   *   phaser.register();
145 < *   for (Runnable task : tasks) {
145 > *   for (final Runnable task : tasks) {
146   *     phaser.register();
147   *     new Thread() {
148   *       public void run() {
149   *         do {
150   *           task.run();
151   *           phaser.arriveAndAwaitAdvance();
152 < *         } while(!phaser.isTerminated();
152 > *         } while (!phaser.isTerminated());
153   *       }
154   *     }.start();
155   *   }
# Line 159 | Line 158 | import java.util.concurrent.locks.LockSu
158   *
159   * If the main task must later await termination, it
160   * may re-register and then execute a similar loop:
161 < * <pre> {@code
161 > *  <pre> {@code
162   *   // ...
163   *   phaser.register();
164   *   while (!phaser.isTerminated())
165 < *     phaser.arriveAndAwaitAdvance();
167 < * }</pre>
165 > *     phaser.arriveAndAwaitAdvance();}</pre>
166   *
167 < * Related constructions may be used to await particular phase numbers
167 > * <p>Related constructions may be used to await particular phase numbers
168   * in contexts where you are sure that the phase will never wrap around
169   * {@code Integer.MAX_VALUE}. For example:
170   *
171 < * <pre> {@code
172 < *   void awaitPhase(Phaser phaser, int phase) {
173 < *     int p = phaser.register(); // assumes caller not already registered
174 < *     while (p < phase) {
175 < *       if (phaser.isTerminated())
176 < *         // ... deal with unexpected termination
177 < *       else
178 < *         p = phaser.arriveAndAwaitAdvance();
181 < *     }
182 < *     phaser.arriveAndDeregister();
171 > *  <pre> {@code
172 > * void awaitPhase(Phaser phaser, int phase) {
173 > *   int p = phaser.register(); // assumes caller not already registered
174 > *   while (p < phase) {
175 > *     if (phaser.isTerminated())
176 > *       // ... deal with unexpected termination
177 > *     else
178 > *       p = phaser.arriveAndAwaitAdvance();
179   *   }
180 < * }</pre>
180 > *   phaser.arriveAndDeregister();
181 > * }}</pre>
182   *
183   *
184   * <p>To create a set of tasks using a tree of phasers,
185   * you could use code of the following form, assuming a
186   * Task class with a constructor accepting a phaser that
187   * it registers for upon construction:
188 + *
189   *  <pre> {@code
190 < * void build(Task[] actions, int lo, int hi, Phaser b) {
191 < *   int step = (hi - lo) / TASKS_PER_PHASER;
192 < *   if (step > 1) {
193 < *     int i = lo;
194 < *     while (i < hi) {
197 < *       int r = Math.min(i + step, hi);
198 < *       build(actions, i, r, new Phaser(b));
199 < *       i = r;
190 > * void build(Task[] actions, int lo, int hi, Phaser ph) {
191 > *   if (hi - lo > TASKS_PER_PHASER) {
192 > *     for (int i = lo; i < hi; i += TASKS_PER_PHASER) {
193 > *       int j = Math.min(i + TASKS_PER_PHASER, hi);
194 > *       build(actions, i, j, new Phaser(ph));
195   *     }
196   *   } else {
197   *     for (int i = lo; i < hi; ++i)
198 < *       actions[i] = new Task(b);
199 < *       // assumes new Task(b) performs b.register()
198 > *       actions[i] = new Task(ph);
199 > *       // assumes new Task(ph) performs ph.register()
200   *   }
201   * }
202   * // .. initially called, for n tasks via
# Line 250 | Line 245 | public class Phaser {
245       */
246      private volatile long state;
247  
253    private static final int ushortBits = 16;
248      private static final int ushortMask = 0xffff;
249      private static final int phaseMask  = 0x7fffffff;
250  
# Line 764 | Line 758 | public class Phaser {
758      }
759  
760      /**
761 <     * Overridable method to perform an action upon phase advance, and
762 <     * to control termination. This method is invoked whenever the
763 <     * barrier is tripped (and thus all other waiting parties are
764 <     * dormant). If it returns {@code true}, then, rather than advance
765 <     * the phase number, this barrier will be set to a final
766 <     * termination state, and subsequent calls to {@link #isTerminated}
767 <     * will return true.
761 >     * Overridable method to perform an action upon impending phase
762 >     * advance, and to control termination. This method is invoked
763 >     * upon arrival of the party tripping the barrier (when all other
764 >     * waiting parties are dormant).  If this method returns {@code
765 >     * true}, then, rather than advance the phase number, this barrier
766 >     * will be set to a final termination state, and subsequent calls
767 >     * to {@link #isTerminated} will return true. Any (unchecked)
768 >     * Exception or Error thrown by an invocation of this method is
769 >     * propagated to the party attempting to trip the barrier, in
770 >     * which case no advance occurs.
771 >     *
772 >     * <p>The arguments to this method provide the state of the phaser
773 >     * prevailing for the current transition. (When called from within
774 >     * an implementation of {@code onAdvance} the values returned by
775 >     * methods such as {@code getPhase} may or may not reliably
776 >     * indicate the state to which this transition applies.)
777       *
778       * <p>The default version returns {@code true} when the number of
779       * registered parties is zero. Normally, overrides that arrange
# Line 778 | Line 781 | public class Phaser {
781       * property.
782       *
783       * <p>You may override this method to perform an action with side
784 <     * effects visible to participating tasks, but it is in general
785 <     * only sensible to do so in designs where all parties register
786 <     * before any arrive, and all {@link #awaitAdvance} at each phase.
784 >     * effects visible to participating tasks, but it is only sensible
785 >     * to do so in designs where all parties register before any
786 >     * arrive, and all {@link #awaitAdvance} at each phase.
787       * Otherwise, you cannot ensure lack of interference from other
788 <     * parties during the invocation of this method.
788 >     * parties during the invocation of this method. Additionally,
789 >     * method {@code onAdvance} may be invoked more than once per
790 >     * transition if registrations are intermixed with arrivals.
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