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

Comparing jsr166/src/jsr166y/CountedCompleter.java (file contents):
Revision 1.16 by jsr166, Sat Nov 24 03:46:28 2012 UTC vs.
Revision 1.25 by jsr166, Mon Nov 26 05:55:05 2012 UTC

# Line 15 | Line 15 | package jsr166y;
15   * CountedCompleter are similar to those of other completion based
16   * components (such as {@link java.nio.channels.CompletionHandler})
17   * except that multiple <em>pending</em> completions may be necessary
18 < * to trigger the {@link #onCompletion} action, not just one. Unless
19 < * initialized otherwise, the {@link #getPendingCount pending count}
20 < * starts at zero, but may be (atomically) changed using methods
21 < * {@link #setPendingCount}, {@link #addToPendingCount}, and {@link
22 < * #compareAndSetPendingCount}. Upon invocation of {@link
18 > * to trigger the completion action {@link #onCompletion}, not just one.
19 > * Unless initialized otherwise, the {@linkplain #getPendingCount pending
20 > * count} starts at zero, but may be (atomically) changed using
21 > * methods {@link #setPendingCount}, {@link #addToPendingCount}, and
22 > * {@link #compareAndSetPendingCount}. Upon invocation of {@link
23   * #tryComplete}, if the pending action count is nonzero, it is
24   * decremented; otherwise, the completion action is performed, and if
25   * this completer itself has a completer, the process is continued
# Line 126 | Line 126 | package jsr166y;
126   *       op.apply(array[lo]);
127   *     tryComplete();
128   *   }
129 < * } }</pre>
129 > * }}</pre>
130   *
131   * This design can be improved by noticing that in the recursive case,
132   * the task has nothing to do after forking its right task, so can
133   * directly invoke its left task before returning. (This is an analog
134   * of tail recursion removal.)  Also, because the task returns upon
135   * executing its left task (rather than falling through to invoke
136 < * tryComplete) the pending count is set to one:
136 > * {@code tryComplete}) the pending count is set to one:
137   *
138   * <pre> {@code
139   * class ForEach<E> ...
# Line 277 | Line 277 | package jsr166y;
277   *   }
278   *   public void onCompletion(CountedCompleter<?> caller) {
279   *     if (caller != this) {
280 < *      MapReducer<E> child = (MapReducer<E>)caller;
281 < *      MapReducer<E> sib = child.sibling;
282 < *      if (sib == null || sib.result == null)
283 < *        result = child.result;
284 < *      else
285 < *        result = reducer.apply(child.result, sib.result);
280 > *       MapReducer<E> child = (MapReducer<E>)caller;
281 > *       MapReducer<E> sib = child.sibling;
282 > *       if (sib == null || sib.result == null)
283 > *         result = child.result;
284 > *       else
285 > *         result = reducer.apply(child.result, sib.result);
286   *     }
287   *   }
288   *   public E getRawResult() { return result; }
# Line 291 | Line 291 | package jsr166y;
291   *     return new MapReducer<E>(null, array, mapper, reducer,
292   *                              0, array.length).invoke();
293   *   }
294 < * } }</pre>
294 > * }}</pre>
295   *
296   * Here, method {@code onCompletion} takes a form common to many
297   * completion designs that combine results. This callback-style method
# Line 303 | Line 303 | package jsr166y;
303   * distinguishes cases.  Most often, when the caller is {@code this},
304   * no action is necessary. Otherwise the caller argument can be used
305   * (usually via a cast) to supply a value (and/or links to other
306 < * values) to be combined.  Asuuming proper use of pending counts, the
306 > * values) to be combined.  Assuming proper use of pending counts, the
307   * actions inside {@code onCompletion} occur (once) upon completion of
308   * a task and its subtasks. No additional synchronization is required
309   * within this method to ensure thread safety of accesses to fields of
# Line 388 | Line 388 | public abstract class CountedCompleter<T
388       * Creates a new CountedCompleter with the given completer
389       * and initial pending count.
390       *
391 <     * @param completer this tasks completer, or {@code null} if none
391 >     * @param completer this task's completer, or {@code null} if none
392       * @param initialPendingCount the initial pending count
393       */
394      protected CountedCompleter(CountedCompleter<?> completer,
# Line 401 | Line 401 | public abstract class CountedCompleter<T
401       * Creates a new CountedCompleter with the given completer
402       * and an initial pending count of zero.
403       *
404 <     * @param completer this tasks completer, or {@code null} if none
404 >     * @param completer this task's completer, or {@code null} if none
405       */
406      protected CountedCompleter(CountedCompleter<?> completer) {
407          this.completer = completer;
# Line 422 | Line 422 | public abstract class CountedCompleter<T
422  
423      /**
424       * Performs an action when method {@link #tryComplete} is invoked
425 <     * and there are no pending counts, or when the unconditional
425 >     * and the pending count is zero, or when the unconditional
426       * method {@link #complete} is invoked.  By default, this method
427       * does nothing. You can distinguish cases by checking the
428       * identity of the given caller argument. If not equal to {@code
# Line 450 | Line 450 | public abstract class CountedCompleter<T
450       * @param caller the task invoking this method (which may
451       * be this task itself).
452       * @return true if this exception should be propagated to this
453 <     * tasks completer, if one exists.
453 >     * task's completer, if one exists.
454       */
455      public boolean onExceptionalCompletion(Throwable ex, CountedCompleter<?> caller) {
456          return true;
# Line 626 | Line 626 | public abstract class CountedCompleter<T
626       * this task's pending count is non-zero, decrements its pending
627       * count and returns {@code null}.  Otherwise, returns the
628       * completer.  This method can be used as part of a completion
629 <     * traversal loop for homogenous task hierarchies:
629 >     * traversal loop for homogeneous task hierarchies:
630       *
631       * <pre> {@code
632 <     * for (CountedCompleter<?> c = firstComplete(); c != null; c = c.nextComplete()) {
632 >     * for (CountedCompleter<?> c = firstComplete();
633 >     *      c != null;
634 >     *      c = c.nextComplete()) {
635       *   // ... process c ...
636       * }}</pre>
637       *
# Line 659 | Line 661 | public abstract class CountedCompleter<T
661      }
662  
663      /**
664 <     * Support for FJT exception propagation
664 >     * Supports ForkJoinTask exception propagation.
665       */
666      void internalPropagateException(Throwable ex) {
667          CountedCompleter<?> a = this, s = a;
# Line 669 | Line 671 | public abstract class CountedCompleter<T
671      }
672  
673      /**
674 <     * Implements execution conventions for CountedCompleters
674 >     * Implements execution conventions for CountedCompleters.
675       */
676      protected final boolean exec() {
677          compute();

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines