7 |
|
package jsr166e; |
8 |
|
|
9 |
|
/** |
10 |
< |
* A resultless {@link ForkJoinTask} with a completion action |
10 |
> |
* A {@link ForkJoinTask} with a completion action |
11 |
|
* performed when triggered and there are no remaining pending |
12 |
|
* actions. Uses of CountedCompleter are similar to those of other |
13 |
|
* completion based components (such as {@link |
22 |
|
* decremented; otherwise, the completion action is performed, and if |
23 |
|
* this completer itself has a completer, the process is continued |
24 |
|
* with its completer. As is the case with related synchronization |
25 |
< |
* components such as {@link Phaser} and {@link |
26 |
< |
* java.util.concurrent.Semaphore} these methods affect only internal |
27 |
< |
* counts; they do not establish any further internal bookkeeping. In |
28 |
< |
* particular, the identities of pending tasks are not maintained. As |
29 |
< |
* illustrated below, you can create subclasses that do record some or |
30 |
< |
* all pended tasks or their results when needed. |
25 |
> |
* components such as {@link java.util.concurrent.Phaser Phaser} and |
26 |
> |
* {@link java.util.concurrent.Semaphore Semaphore}, these methods |
27 |
> |
* affect only internal counts; they do not establish any further |
28 |
> |
* internal bookkeeping. In particular, the identities of pending |
29 |
> |
* tasks are not maintained. As illustrated below, you can create |
30 |
> |
* subclasses that do record some or all pending tasks or their |
31 |
> |
* results when needed. |
32 |
|
* |
33 |
|
* <p>A concrete CountedCompleter class must define method {@link |
34 |
|
* #compute}, that should, in almost all use cases, invoke {@code |
37 |
|
* normal completion, and method {@link #onExceptionalCompletion} to |
38 |
|
* perform an action upon any exception. |
39 |
|
* |
40 |
+ |
* <p>CountedCompleters most often do not bear results, in which case |
41 |
+ |
* they are normally declared as {@code CountedCompleter<Void>}, and |
42 |
+ |
* will always return {@code null} as a result value. In other cases, |
43 |
+ |
* you should override method {@link #getRawResult} to provide a |
44 |
+ |
* result from {@code join(), invoke()}, and related methods. (Method |
45 |
+ |
* {@link #setRawResult} by default plays no role in CountedCompleters |
46 |
+ |
* but may be overridden for example to maintain fields holding result |
47 |
+ |
* data.) |
48 |
+ |
* |
49 |
|
* <p>A CountedCompleter that does not itself have a completer (i.e., |
50 |
|
* one for which {@link #getCompleter} returns {@code null}) can be |
51 |
|
* used as a regular ForkJoinTask with this added functionality. |
65 |
|
* <p><b>Parallel recursive decomposition.</b> CountedCompleters may |
66 |
|
* be arranged in trees similar to those often used with {@link |
67 |
|
* RecursiveAction}s, although the constructions involved in setting |
68 |
< |
* them up typically vary. Even though they entail a bit more |
68 |
> |
* them up typically vary. Here, the completer of each task is its |
69 |
> |
* parent in the computation tree. Even though they entail a bit more |
70 |
|
* bookkeeping, CountedCompleters may be better choices when applying |
71 |
|
* a possibly time-consuming operation (that cannot be further |
72 |
|
* subdivided) to each element of an array or collection; especially |
91 |
|
* <pre> {@code |
92 |
|
* class MyOperation<E> { void apply(E e) { ... } } |
93 |
|
* |
94 |
< |
* class ForEach<E> extends CountedCompleter { |
94 |
> |
* class ForEach<E> extends CountedCompleter<Void> { |
95 |
|
* |
96 |
|
* public static <E> void forEach(ForkJoinPool pool, E[] array, MyOperation<E> op) { |
97 |
|
* pool.invoke(new ForEach<E>(null, array, op, 0, array.length)); |
98 |
|
* } |
99 |
|
* |
100 |
|
* final E[] array; final MyOperation<E> op; final int lo, hi; |
101 |
< |
* ForEach(CountedCompleter p, E[] array, MyOperation<E> op, int lo, int hi) { |
101 |
> |
* ForEach(CountedCompleter<?> p, E[] array, MyOperation<E> op, int lo, int hi) { |
102 |
|
* super(p); |
103 |
|
* this.array = array; this.op = op; this.lo = lo; this.hi = hi; |
104 |
|
* } |
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: |
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. |
148 |
|
* |
149 |
|
* <pre> {@code |
150 |
|
* class ForEach<E> ... |
158 |
|
* } |
159 |
|
* if (h > l) |
160 |
|
* op.apply(array[l]); |
161 |
< |
* tryComplete(); |
161 |
> |
* helpComplete(); |
162 |
|
* } |
163 |
|
* }</pre> |
164 |
|
* |
175 |
|
* and reductions are all of type {@code E}), one way to do this in |
176 |
|
* divide and conquer designs is to have each subtask record its |
177 |
|
* sibling, so that it can be accessed in method {@code onCompletion}. |
178 |
< |
* For clarity, this class uses explicit left and right subtasks, but |
179 |
< |
* variants of other streamlinings seen in the above example may also |
180 |
< |
* apply. |
178 |
> |
* This technique applies to reductions in which the order of |
179 |
> |
* combining left and right results does not matter; ordered |
180 |
> |
* reductions require explicit left/right designations. Variants of |
181 |
> |
* other streamlinings seen in the above examples may also apply. |
182 |
|
* |
183 |
|
* <pre> {@code |
184 |
|
* class MyMapper<E> { E apply(E v) { ... } } |
185 |
|
* class MyReducer<E> { E apply(E x, E y) { ... } } |
186 |
< |
* class MapReducer<E> extends CountedCompleter { |
186 |
> |
* class MapReducer<E> extends CountedCompleter<E> { |
187 |
|
* final E[] array; final MyMapper<E> mapper; |
188 |
|
* final MyReducer<E> reducer; final int lo, hi; |
189 |
< |
* MapReducer sibling; |
189 |
> |
* MapReducer<E> sibling; |
190 |
|
* E result; |
191 |
|
* MapReducer(CountedCompleter p, E[] array, MyMapper<E> mapper, |
192 |
|
* MyReducer<E> reducer, int lo, int hi) { |
221 |
|
* result = reducer.apply(child.result, sib.result); |
222 |
|
* } |
223 |
|
* } |
224 |
+ |
* public E getRawResult() { return result; } |
225 |
|
* |
226 |
|
* public static <E> E mapReduce(ForkJoinPool pool, E[] array, |
227 |
|
* MyMapper<E> mapper, MyReducer<E> reducer) { |
228 |
< |
* MapReducer<E> mr = new MapReducer<E>(null, array, mapper, |
229 |
< |
* reducer, 0, array.length); |
215 |
< |
* pool.invoke(mr); |
216 |
< |
* return mr.result; |
228 |
> |
* return pool.invoke(new MapReducer<E>(null, array, mapper, |
229 |
> |
* reducer, 0, array.length)); |
230 |
|
* } |
231 |
|
* } }</pre> |
232 |
|
* |
236 |
|
* triggers another async task. For example: |
237 |
|
* |
238 |
|
* <pre> {@code |
239 |
< |
* class HeaderBuilder extends CountedCompleter { ... } |
240 |
< |
* class BodyBuilder extends CountedCompleter { ... } |
241 |
< |
* class PacketSender extends CountedCompleter { |
239 |
> |
* class HeaderBuilder extends CountedCompleter<...> { ... } |
240 |
> |
* class BodyBuilder extends CountedCompleter<...> { ... } |
241 |
> |
* class PacketSender extends CountedCompleter<...> { |
242 |
|
* PacketSender(...) { super(null, 1); ... } // trigger on second completion |
243 |
|
* public void compute() { } // never called |
244 |
< |
* public void onCompletion(CountedCompleter caller) { sendPacket(); } |
244 |
> |
* public void onCompletion(CountedCompleter<?> caller) { sendPacket(); } |
245 |
|
* } |
246 |
|
* // sample use: |
247 |
|
* PacketSender p = new PacketSender(); |
252 |
|
* @since 1.8 |
253 |
|
* @author Doug Lea |
254 |
|
*/ |
255 |
< |
public abstract class CountedCompleter extends ForkJoinTask<Void> { |
255 |
> |
public abstract class CountedCompleter<T> extends ForkJoinTask<T> { |
256 |
|
private static final long serialVersionUID = 5232453752276485070L; |
257 |
|
|
258 |
|
/** This task's completer, or null if none */ |
259 |
< |
final CountedCompleter completer; |
259 |
> |
final CountedCompleter<?> completer; |
260 |
|
/** The number of pending tasks until completion */ |
261 |
|
volatile int pending; |
262 |
|
|
267 |
|
* @param completer this tasks completer, or {@code null} if none |
268 |
|
* @param initialPendingCount the initial pending count |
269 |
|
*/ |
270 |
< |
protected CountedCompleter(CountedCompleter completer, |
270 |
> |
protected CountedCompleter(CountedCompleter<?> completer, |
271 |
|
int initialPendingCount) { |
272 |
|
this.completer = completer; |
273 |
|
this.pending = initialPendingCount; |
279 |
|
* |
280 |
|
* @param completer this tasks completer, or {@code null} if none |
281 |
|
*/ |
282 |
< |
protected CountedCompleter(CountedCompleter completer) { |
282 |
> |
protected CountedCompleter(CountedCompleter<?> completer) { |
283 |
|
this.completer = completer; |
284 |
|
} |
285 |
|
|
305 |
|
* @param caller the task invoking this method (which may |
306 |
|
* be this task itself). |
307 |
|
*/ |
308 |
< |
public void onCompletion(CountedCompleter caller) { |
308 |
> |
public void onCompletion(CountedCompleter<?> caller) { |
309 |
|
} |
310 |
|
|
311 |
|
/** |
325 |
|
* @return true if this exception should be propagated to this |
326 |
|
* tasks completer, if one exists. |
327 |
|
*/ |
328 |
< |
public boolean onExceptionalCompletion(Throwable ex, CountedCompleter caller) { |
328 |
> |
public boolean onExceptionalCompletion(Throwable ex, CountedCompleter<?> caller) { |
329 |
|
return true; |
330 |
|
} |
331 |
|
|
335 |
|
* |
336 |
|
* @return the completer |
337 |
|
*/ |
338 |
< |
public final CountedCompleter getCompleter() { |
338 |
> |
public final CountedCompleter<?> getCompleter() { |
339 |
|
return completer; |
340 |
|
} |
341 |
|
|
380 |
|
} |
381 |
|
|
382 |
|
/** |
383 |
+ |
* Returns the root of the current computation; i.e., this |
384 |
+ |
* task if it has no completer, else its completer's root. |
385 |
+ |
* |
386 |
+ |
* @return the root of the current computation |
387 |
+ |
*/ |
388 |
+ |
public final CountedCompleter<?> getRoot() { |
389 |
+ |
CountedCompleter<?> a = this, p; |
390 |
+ |
while ((p = a.completer) != null) |
391 |
+ |
a = p; |
392 |
+ |
return a; |
393 |
+ |
} |
394 |
+ |
|
395 |
+ |
/** |
396 |
|
* If the pending count is nonzero, decrements the count; |
397 |
|
* otherwise invokes {@link #onCompletion} and then similarly |
398 |
|
* tries to complete this task's completer, if one exists, |
399 |
|
* else marks this task as complete. |
400 |
|
*/ |
401 |
|
public final void tryComplete() { |
402 |
< |
CountedCompleter a = this, s = a; |
402 |
> |
CountedCompleter<?> a = this, s = a; |
403 |
|
for (int c;;) { |
404 |
|
if ((c = a.pending) == 0) { |
405 |
|
a.onCompletion(s); |
414 |
|
} |
415 |
|
|
416 |
|
/** |
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 |
+ |
/** |
440 |
|
* Regardless of pending count, invokes {@link #onCompletion}, |
441 |
< |
* marks this task as complete with a {@code null} return value, |
442 |
< |
* and further triggers {@link #tryComplete} on this task's |
443 |
< |
* completer, if one exists. This method may be useful when |
444 |
< |
* forcing completion as soon as any one (versus all) of several |
445 |
< |
* subtask results are obtained. |
441 |
> |
* marks this task as complete and further triggers {@link |
442 |
> |
* #tryComplete} on this task's completer, if one exists. This |
443 |
> |
* method may be useful when forcing completion as soon as any one |
444 |
> |
* (versus all) of several subtask results are obtained. The |
445 |
> |
* given rawResult is used as an argument to {@link #setRawResult} |
446 |
> |
* before marking this task as complete; its value is meaningful |
447 |
> |
* only for classes overriding {@code setRawResult}. |
448 |
|
* |
449 |
< |
* @param mustBeNull the {@code null} completion value |
449 |
> |
* @param rawResult the raw result |
450 |
|
*/ |
451 |
< |
public void complete(Void mustBeNull) { |
452 |
< |
CountedCompleter p; |
451 |
> |
public void complete(T rawResult) { |
452 |
> |
CountedCompleter<?> p; |
453 |
|
onCompletion(this); |
454 |
+ |
setRawResult(rawResult); |
455 |
|
quietlyComplete(); |
456 |
|
if ((p = completer) != null) |
457 |
|
p.tryComplete(); |
461 |
|
* Support for FJT exception propagation |
462 |
|
*/ |
463 |
|
void internalPropagateException(Throwable ex) { |
464 |
< |
CountedCompleter a = this, s = a; |
464 |
> |
CountedCompleter<?> a = this, s = a; |
465 |
|
while (a.onExceptionalCompletion(ex, s) && |
466 |
|
(a = (s = a).completer) != null && a.status >= 0) |
467 |
|
a.recordExceptionalCompletion(ex); |
476 |
|
} |
477 |
|
|
478 |
|
/** |
479 |
< |
* Always returns {@code null}. |
479 |
> |
* Returns the result of the computation. By default |
480 |
> |
* returns {@code null}, which is appropriate for {@code Void} |
481 |
> |
* actions, but in other cases should be overridden. |
482 |
|
* |
483 |
< |
* @return {@code null} always |
483 |
> |
* @return the result of the computation |
484 |
|
*/ |
485 |
< |
public final Void getRawResult() { return null; } |
485 |
> |
public T getRawResult() { return null; } |
486 |
|
|
487 |
|
/** |
488 |
< |
* Requires null completion value. |
488 |
> |
* A method that result-bearing CountedCompleters may optionally |
489 |
> |
* use to help maintain result data. By default, does nothing. |
490 |
|
*/ |
491 |
< |
protected final void setRawResult(Void mustBeNull) { } |
491 |
> |
protected void setRawResult(T t) { } |
492 |
|
|
493 |
|
// Unsafe mechanics |
494 |
|
private static final sun.misc.Unsafe U; |
503 |
|
} |
504 |
|
} |
505 |
|
|
451 |
– |
|
506 |
|
/** |
507 |
|
* Returns a sun.misc.Unsafe. Suitable for use in a 3rd party package. |
508 |
|
* Replace with a simple call to Unsafe.getUnsafe when integrating |