77 |
|
* continuations, other threads need not block waiting to perform |
78 |
|
* them. |
79 |
|
* |
80 |
< |
* <p> For example, here is an initial version of a class that uses |
80 |
> |
* <p>For example, here is an initial version of a class that uses |
81 |
|
* divide-by-two recursive decomposition to divide work into single |
82 |
|
* pieces (leaf tasks). Even when work is split into individual calls, |
83 |
|
* tree-based techniques are usually preferable to directly forking |
142 |
|
* |
143 |
|
* As a further improvement, notice that the left task need not even |
144 |
|
* exist. Instead of creating a new one, we can iterate using the |
145 |
< |
* original task, and add a pending count for each fork. Additionally, |
146 |
< |
* this version uses {@code helpComplete} to streamline assistance in |
147 |
< |
* the execution of forked tasks. |
145 |
> |
* original task, and add a pending count for each fork. |
146 |
|
* |
147 |
|
* <pre> {@code |
148 |
|
* class ForEach<E> ... |
156 |
|
* } |
157 |
|
* if (h > l) |
158 |
|
* op.apply(array[l]); |
159 |
< |
* helpComplete(); |
159 |
> |
* tryComplete(); |
160 |
|
* } |
161 |
|
* }</pre> |
162 |
|
* |
412 |
|
} |
413 |
|
|
414 |
|
/** |
417 |
– |
* Identical to {@link #tryComplete}, but may additionally execute |
418 |
– |
* other tasks within the current computation (i.e., those |
419 |
– |
* with the same {@link #getRoot}. |
420 |
– |
*/ |
421 |
– |
public final void helpComplete() { |
422 |
– |
CountedCompleter<?> a = this, s = a; |
423 |
– |
for (int c;;) { |
424 |
– |
if ((c = a.pending) == 0) { |
425 |
– |
a.onCompletion(s); |
426 |
– |
if ((a = (s = a).completer) == null) { |
427 |
– |
s.quietlyComplete(); |
428 |
– |
return; |
429 |
– |
} |
430 |
– |
} |
431 |
– |
else if (U.compareAndSwapInt(a, PENDING, c, c - 1)) { |
432 |
– |
if (!(Thread.currentThread() instanceof ForkJoinWorkerThread)) |
433 |
– |
ForkJoinPool.popAndExecCCFromCommonPool(a); |
434 |
– |
return; |
435 |
– |
} |
436 |
– |
} |
437 |
– |
} |
438 |
– |
|
439 |
– |
/** |
415 |
|
* Regardless of pending count, invokes {@link #onCompletion}, |
416 |
|
* marks this task as complete and further triggers {@link |
417 |
|
* #tryComplete} on this task's completer, if one exists. This |
505 |
|
} |
506 |
|
} |
507 |
|
} |
533 |
– |
|
508 |
|
} |