12 |
|
import java.util.Collection; |
13 |
|
import java.util.Collections; |
14 |
|
import java.util.List; |
15 |
+ |
import java.util.RandomAccess; |
16 |
|
import java.util.Map; |
17 |
|
import java.util.WeakHashMap; |
18 |
|
|
19 |
|
/** |
20 |
< |
* Abstract base class for tasks that run within a {@link |
21 |
< |
* ForkJoinPool}. A ForkJoinTask is a thread-like entity that is much |
20 |
> |
* Abstract base class for tasks that run within a {@link ForkJoinPool}. |
21 |
> |
* A {@code ForkJoinTask} is a thread-like entity that is much |
22 |
|
* lighter weight than a normal thread. Huge numbers of tasks and |
23 |
|
* subtasks may be hosted by a small number of actual threads in a |
24 |
|
* ForkJoinPool, at the price of some usage limitations. |
25 |
|
* |
26 |
< |
* <p> A "main" ForkJoinTask begins execution when submitted to a |
27 |
< |
* {@link ForkJoinPool}. Once started, it will usually in turn start |
28 |
< |
* other subtasks. As indicated by the name of this class, many |
29 |
< |
* programs using ForkJoinTasks employ only methods {@code fork} |
30 |
< |
* and {@code join}, or derivatives such as |
31 |
< |
* {@code invokeAll}. However, this class also provides a number |
32 |
< |
* of other methods that can come into play in advanced usages, as |
33 |
< |
* well as extension mechanics that allow support of new forms of |
34 |
< |
* fork/join processing. |
26 |
> |
* <p>A "main" {@code ForkJoinTask} begins execution when submitted |
27 |
> |
* to a {@link ForkJoinPool}. Once started, it will usually in turn |
28 |
> |
* start other subtasks. As indicated by the name of this class, |
29 |
> |
* many programs using {@code ForkJoinTask} employ only methods |
30 |
> |
* {@link #fork} and {@link #join}, or derivatives such as {@link |
31 |
> |
* #invokeAll}. However, this class also provides a number of other |
32 |
> |
* methods that can come into play in advanced usages, as well as |
33 |
> |
* extension mechanics that allow support of new forms of fork/join |
34 |
> |
* processing. |
35 |
|
* |
36 |
< |
* <p>A ForkJoinTask is a lightweight form of {@link Future}. The |
37 |
< |
* efficiency of ForkJoinTasks stems from a set of restrictions (that |
38 |
< |
* are only partially statically enforceable) reflecting their |
39 |
< |
* intended use as computational tasks calculating pure functions or |
40 |
< |
* operating on purely isolated objects. The primary coordination |
41 |
< |
* mechanisms are {@link #fork}, that arranges asynchronous execution, |
42 |
< |
* and {@link #join}, that doesn't proceed until the task's result has |
43 |
< |
* been computed. Computations should avoid {@code synchronized} |
44 |
< |
* methods or blocks, and should minimize other blocking |
45 |
< |
* synchronization apart from joining other tasks or using |
46 |
< |
* synchronizers such as Phasers that are advertised to cooperate with |
47 |
< |
* fork/join scheduling. Tasks should also not perform blocking IO, |
48 |
< |
* and should ideally access variables that are completely independent |
49 |
< |
* of those accessed by other running tasks. Minor breaches of these |
50 |
< |
* restrictions, for example using shared output streams, may be |
51 |
< |
* tolerable in practice, but frequent use may result in poor |
52 |
< |
* performance, and the potential to indefinitely stall if the number |
53 |
< |
* of threads not waiting for IO or other external synchronization |
54 |
< |
* becomes exhausted. This usage restriction is in part enforced by |
55 |
< |
* not permitting checked exceptions such as {@code IOExceptions} |
56 |
< |
* to be thrown. However, computations may still encounter unchecked |
57 |
< |
* exceptions, that are rethrown to callers attempting join |
58 |
< |
* them. These exceptions may additionally include |
59 |
< |
* RejectedExecutionExceptions stemming from internal resource |
60 |
< |
* exhaustion such as failure to allocate internal task queues. |
36 |
> |
* <p>A {@code ForkJoinTask} is a lightweight form of {@link Future}. |
37 |
> |
* The efficiency of {@code ForkJoinTask}s stems from a set of |
38 |
> |
* restrictions (that are only partially statically enforceable) |
39 |
> |
* reflecting their intended use as computational tasks calculating |
40 |
> |
* pure functions or operating on purely isolated objects. The |
41 |
> |
* primary coordination mechanisms are {@link #fork}, that arranges |
42 |
> |
* asynchronous execution, and {@link #join}, that doesn't proceed |
43 |
> |
* until the task's result has been computed. Computations should |
44 |
> |
* avoid {@code synchronized} methods or blocks, and should minimize |
45 |
> |
* other blocking synchronization apart from joining other tasks or |
46 |
> |
* using synchronizers such as Phasers that are advertised to |
47 |
> |
* cooperate with fork/join scheduling. Tasks should also not perform |
48 |
> |
* blocking IO, and should ideally access variables that are |
49 |
> |
* completely independent of those accessed by other running |
50 |
> |
* tasks. Minor breaches of these restrictions, for example using |
51 |
> |
* shared output streams, may be tolerable in practice, but frequent |
52 |
> |
* use may result in poor performance, and the potential to |
53 |
> |
* indefinitely stall if the number of threads not waiting for IO or |
54 |
> |
* other external synchronization becomes exhausted. This usage |
55 |
> |
* restriction is in part enforced by not permitting checked |
56 |
> |
* exceptions such as {@code IOExceptions} to be thrown. However, |
57 |
> |
* computations may still encounter unchecked exceptions, that are |
58 |
> |
* rethrown to callers attempting to join them. These exceptions may |
59 |
> |
* additionally include RejectedExecutionExceptions stemming from |
60 |
> |
* internal resource exhaustion such as failure to allocate internal |
61 |
> |
* task queues. |
62 |
|
* |
63 |
|
* <p>The primary method for awaiting completion and extracting |
64 |
|
* results of a task is {@link #join}, but there are several variants: |
68 |
|
* execute other tasks while awaiting joins, which is sometimes more |
69 |
|
* efficient but only applies when all subtasks are known to be |
70 |
|
* strictly tree-structured. Method {@link #invoke} is semantically |
71 |
< |
* equivalent to {@code fork(); join()} but always attempts to |
72 |
< |
* begin execution in the current thread. The "<em>quiet</em>" forms |
73 |
< |
* of these methods do not extract results or report exceptions. These |
71 |
> |
* equivalent to {@code fork(); join()} but always attempts to begin |
72 |
> |
* execution in the current thread. The "<em>quiet</em>" forms of |
73 |
> |
* these methods do not extract results or report exceptions. These |
74 |
|
* may be useful when a set of tasks are being executed, and you need |
75 |
|
* to delay processing of results or exceptions until all complete. |
76 |
|
* Method {@code invokeAll} (available in multiple versions) |
77 |
|
* performs the most common form of parallel invocation: forking a set |
78 |
|
* of tasks and joining them all. |
79 |
|
* |
80 |
< |
* <p> The ForkJoinTask class is not usually directly subclassed. |
80 |
> |
* <p>The execution status of tasks may be queried at several levels |
81 |
> |
* of detail: {@link #isDone} is true if a task completed in any way |
82 |
> |
* (including the case where a task was cancelled without executing); |
83 |
> |
* {@link #isCancelled} is true if completion was due to cancellation; |
84 |
> |
* {@link #isCompletedNormally} is true if a task completed without |
85 |
> |
* cancellation or encountering an exception; {@link |
86 |
> |
* #isCompletedExceptionally} is true if if the task encountered an |
87 |
> |
* exception (in which case {@link #getException} returns the |
88 |
> |
* exception); {@link #isCancelled} is true if the task was cancelled |
89 |
> |
* (in which case {@link #getException} returns a {@link |
90 |
> |
* java.util.concurrent.CancellationException}); and {@link |
91 |
> |
* #isCompletedAbnormally} is true if a task was either cancelled or |
92 |
> |
* encountered an exception. |
93 |
> |
* |
94 |
> |
* <p>The ForkJoinTask class is not usually directly subclassed. |
95 |
|
* Instead, you subclass one of the abstract classes that support a |
96 |
< |
* particular style of fork/join processing. Normally, a concrete |
96 |
> |
* particular style of fork/join processing, typically {@link |
97 |
> |
* RecursiveAction} for computations that do not return results, or |
98 |
> |
* {@link RecursiveTask} for those that do. Normally, a concrete |
99 |
|
* ForkJoinTask subclass declares fields comprising its parameters, |
100 |
|
* established in a constructor, and then defines a {@code compute} |
101 |
|
* method that somehow uses the control methods supplied by this base |
102 |
|
* class. While these methods have {@code public} access (to allow |
103 |
< |
* instances of different task subclasses to call each others |
103 |
> |
* instances of different task subclasses to call each other's |
104 |
|
* methods), some of them may only be called from within other |
105 |
|
* ForkJoinTasks (as may be determined using method {@link |
106 |
|
* #inForkJoinPool}). Attempts to invoke them in other contexts |
107 |
|
* result in exceptions or errors, possibly including |
108 |
|
* ClassCastException. |
109 |
|
* |
110 |
< |
* <p>Most base support methods are {@code final} because their |
111 |
< |
* implementations are intrinsically tied to the underlying |
112 |
< |
* lightweight task scheduling framework, and so cannot be overridden. |
113 |
< |
* Developers creating new basic styles of fork/join processing should |
114 |
< |
* minimally implement {@code protected} methods |
115 |
< |
* {@code exec}, {@code setRawResult}, and |
116 |
< |
* {@code getRawResult}, while also introducing an abstract |
117 |
< |
* computational method that can be implemented in its subclasses, |
118 |
< |
* possibly relying on other {@code protected} methods provided |
101 |
< |
* by this class. |
110 |
> |
* <p>Most base support methods are {@code final}, to prevent |
111 |
> |
* overriding of implementations that are intrinsically tied to the |
112 |
> |
* underlying lightweight task scheduling framework. Developers |
113 |
> |
* creating new basic styles of fork/join processing should minimally |
114 |
> |
* implement {@code protected} methods {@link #exec}, {@link |
115 |
> |
* #setRawResult}, and {@link #getRawResult}, while also introducing |
116 |
> |
* an abstract computational method that can be implemented in its |
117 |
> |
* subclasses, possibly relying on other {@code protected} methods |
118 |
> |
* provided by this class. |
119 |
|
* |
120 |
|
* <p>ForkJoinTasks should perform relatively small amounts of |
121 |
< |
* computations, otherwise splitting into smaller tasks. As a very |
122 |
< |
* rough rule of thumb, a task should perform more than 100 and less |
123 |
< |
* than 10000 basic computational steps. If tasks are too big, then |
124 |
< |
* parallelism cannot improve throughput. If too small, then memory |
125 |
< |
* and internal task maintenance overhead may overwhelm processing. |
121 |
> |
* computation. Large tasks should be split into smaller subtasks, |
122 |
> |
* usually via recursive decomposition. As a very rough rule of thumb, |
123 |
> |
* a task should perform more than 100 and less than 10000 basic |
124 |
> |
* computational steps. If tasks are too big, then parallelism cannot |
125 |
> |
* improve throughput. If too small, then memory and internal task |
126 |
> |
* maintenance overhead may overwhelm processing. |
127 |
> |
* |
128 |
> |
* <p>This class provides {@code adapt} methods for {@link Runnable} |
129 |
> |
* and {@link Callable}, that may be of use when mixing execution of |
130 |
> |
* {@code ForkJoinTasks} with other kinds of tasks. When all tasks |
131 |
> |
* are of this form, consider using a pool in |
132 |
> |
* {@linkplain ForkJoinPool#setAsyncMode async mode}. |
133 |
|
* |
134 |
< |
* <p>ForkJoinTasks are {@code Serializable}, which enables them |
135 |
< |
* to be used in extensions such as remote execution frameworks. It is |
136 |
< |
* in general sensible to serialize tasks only before or after, but |
137 |
< |
* not during execution. Serialization is not relied on during |
114 |
< |
* execution itself. |
134 |
> |
* <p>ForkJoinTasks are {@code Serializable}, which enables them to be |
135 |
> |
* used in extensions such as remote execution frameworks. It is |
136 |
> |
* sensible to serialize tasks only before or after, but not during, |
137 |
> |
* execution. Serialization is not relied on during execution itself. |
138 |
|
* |
139 |
|
* @since 1.7 |
140 |
|
* @author Doug Lea |
271 |
|
synchronized (this) { |
272 |
|
try { |
273 |
|
while (status >= 0) { |
274 |
< |
long nt = nanos - System.nanoTime() - startTime; |
274 |
> |
long nt = nanos - (System.nanoTime() - startTime); |
275 |
|
if (nt <= 0) |
276 |
|
break; |
277 |
|
wait(nt / 1000000, (int) (nt % 1000000)); |
392 |
|
|
393 |
|
/** |
394 |
|
* Returns result or throws exception using j.u.c.Future conventions. |
395 |
< |
* Only call when {@code isDone} known to be true. |
395 |
> |
* Only call when {@code isDone} known to be true or thread known |
396 |
> |
* to be interrupted. |
397 |
|
*/ |
398 |
|
private V reportFutureResult() |
399 |
< |
throws ExecutionException, InterruptedException { |
399 |
> |
throws InterruptedException, ExecutionException { |
400 |
> |
if (Thread.interrupted()) |
401 |
> |
throw new InterruptedException(); |
402 |
|
int s = status & COMPLETION_MASK; |
403 |
|
if (s < NORMAL) { |
404 |
|
Throwable ex; |
406 |
|
throw new CancellationException(); |
407 |
|
if (s == EXCEPTIONAL && (ex = exceptionMap.get(this)) != null) |
408 |
|
throw new ExecutionException(ex); |
383 |
– |
if (Thread.interrupted()) |
384 |
– |
throw new InterruptedException(); |
409 |
|
} |
410 |
|
return getRawResult(); |
411 |
|
} |
416 |
|
*/ |
417 |
|
private V reportTimedFutureResult() |
418 |
|
throws InterruptedException, ExecutionException, TimeoutException { |
419 |
+ |
if (Thread.interrupted()) |
420 |
+ |
throw new InterruptedException(); |
421 |
|
Throwable ex; |
422 |
|
int s = status & COMPLETION_MASK; |
423 |
|
if (s == NORMAL) |
424 |
|
return getRawResult(); |
425 |
< |
if (s == CANCELLED) |
425 |
> |
else if (s == CANCELLED) |
426 |
|
throw new CancellationException(); |
427 |
< |
if (s == EXCEPTIONAL && (ex = exceptionMap.get(this)) != null) |
427 |
> |
else if (s == EXCEPTIONAL && (ex = exceptionMap.get(this)) != null) |
428 |
|
throw new ExecutionException(ex); |
429 |
< |
if (Thread.interrupted()) |
430 |
< |
throw new InterruptedException(); |
405 |
< |
throw new TimeoutException(); |
429 |
> |
else |
430 |
> |
throw new TimeoutException(); |
431 |
|
} |
432 |
|
|
433 |
|
// internal execution methods |
512 |
|
/** |
513 |
|
* Arranges to asynchronously execute this task. While it is not |
514 |
|
* necessarily enforced, it is a usage error to fork a task more |
515 |
< |
* than once unless it has completed and been reinitialized. This |
491 |
< |
* method may be invoked only from within ForkJoinTask |
492 |
< |
* computations (as may be determined using method {@link |
493 |
< |
* #inForkJoinPool}). Attempts to invoke in other contexts result |
494 |
< |
* in exceptions or errors, possibly including ClassCastException. |
515 |
> |
* than once unless it has completed and been reinitialized. |
516 |
|
* |
517 |
< |
* @return {@code this}, to simplify usage. |
517 |
> |
* <p>This method may be invoked only from within {@code |
518 |
> |
* ForkJoinTask} computations (as may be determined using method |
519 |
> |
* {@link #inForkJoinPool}). Attempts to invoke in other contexts |
520 |
> |
* result in exceptions or errors, possibly including {@code |
521 |
> |
* ClassCastException}. |
522 |
> |
* |
523 |
> |
* @return {@code this}, to simplify usage |
524 |
|
*/ |
525 |
|
public final ForkJoinTask<V> fork() { |
526 |
|
((ForkJoinWorkerThread) Thread.currentThread()) |
530 |
|
|
531 |
|
/** |
532 |
|
* Returns the result of the computation when it is ready. |
533 |
< |
* This method differs from {@code get} in that abnormal |
534 |
< |
* completion results in RuntimeExceptions or Errors, not |
535 |
< |
* ExecutionExceptions. |
533 |
> |
* This method differs from {@link #get()} in that |
534 |
> |
* abnormal completion results in {@code RuntimeException} or |
535 |
> |
* {@code Error}, not {@code ExecutionException}. |
536 |
|
* |
537 |
|
* @return the computed result |
538 |
|
*/ |
545 |
|
|
546 |
|
/** |
547 |
|
* Commences performing this task, awaits its completion if |
548 |
< |
* necessary, and return its result. |
548 |
> |
* necessary, and return its result, or throws an (unchecked) |
549 |
> |
* exception if the underlying computation did so. |
550 |
|
* |
523 |
– |
* @throws Throwable (a RuntimeException, Error, or unchecked |
524 |
– |
* exception) if the underlying computation did so |
551 |
|
* @return the computed result |
552 |
|
*/ |
553 |
|
public final V invoke() { |
558 |
|
} |
559 |
|
|
560 |
|
/** |
561 |
< |
* Forks both tasks, returning when {@code isDone} holds for |
562 |
< |
* both of them or an exception is encountered. This method may be |
563 |
< |
* invoked only from within ForkJoinTask computations (as may be |
564 |
< |
* determined using method {@link #inForkJoinPool}). Attempts to |
565 |
< |
* invoke in other contexts result in exceptions or errors, |
566 |
< |
* possibly including ClassCastException. |
567 |
< |
* |
568 |
< |
* @param t1 one task |
569 |
< |
* @param t2 the other task |
570 |
< |
* @throws NullPointerException if t1 or t2 are null |
571 |
< |
* @throws RuntimeException or Error if either task did so |
561 |
> |
* Forks the given tasks, returning when {@code isDone} holds for |
562 |
> |
* each task or an (unchecked) exception is encountered, in which |
563 |
> |
* case the exception is rethrown. If either task encounters an |
564 |
> |
* exception, the other one may be, but is not guaranteed to be, |
565 |
> |
* cancelled. If both tasks throw an exception, then this method |
566 |
> |
* throws one of them. The individual status of each task may be |
567 |
> |
* checked using {@link #getException()} and related methods. |
568 |
> |
* |
569 |
> |
* <p>This method may be invoked only from within {@code |
570 |
> |
* ForkJoinTask} computations (as may be determined using method |
571 |
> |
* {@link #inForkJoinPool}). Attempts to invoke in other contexts |
572 |
> |
* result in exceptions or errors, possibly including {@code |
573 |
> |
* ClassCastException}. |
574 |
> |
* |
575 |
> |
* @param t1 the first task |
576 |
> |
* @param t2 the second task |
577 |
> |
* @throws NullPointerException if any task is null |
578 |
|
*/ |
579 |
< |
public static void invokeAll(ForkJoinTask<?>t1, ForkJoinTask<?> t2) { |
579 |
> |
public static void invokeAll(ForkJoinTask<?> t1, ForkJoinTask<?> t2) { |
580 |
|
t2.fork(); |
581 |
|
t1.invoke(); |
582 |
|
t2.join(); |
583 |
|
} |
584 |
|
|
585 |
|
/** |
586 |
< |
* Forks the given tasks, returning when {@code isDone} holds |
587 |
< |
* for all of them. If any task encounters an exception, others |
588 |
< |
* may be cancelled. This method may be invoked only from within |
589 |
< |
* ForkJoinTask computations (as may be determined using method |
590 |
< |
* {@link #inForkJoinPool}). Attempts to invoke in other contexts |
591 |
< |
* result in exceptions or errors, possibly including |
592 |
< |
* ClassCastException. |
586 |
> |
* Forks the given tasks, returning when {@code isDone} holds for |
587 |
> |
* each task or an (unchecked) exception is encountered, in which |
588 |
> |
* case the exception is rethrown. If any task encounters an |
589 |
> |
* exception, others may be, but are not guaranteed to be, |
590 |
> |
* cancelled. If more than one task encounters an exception, then |
591 |
> |
* this method throws any one of these exceptions. The individual |
592 |
> |
* status of each task may be checked using {@link #getException()} |
593 |
> |
* and related methods. |
594 |
> |
* |
595 |
> |
* <p>This method may be invoked only from within {@code |
596 |
> |
* ForkJoinTask} computations (as may be determined using method |
597 |
> |
* {@link #inForkJoinPool}). Attempts to invoke in other contexts |
598 |
> |
* result in exceptions or errors, possibly including {@code |
599 |
> |
* ClassCastException}. |
600 |
|
* |
601 |
< |
* @param tasks the array of tasks |
602 |
< |
* @throws NullPointerException if tasks or any element are null |
564 |
< |
* @throws RuntimeException or Error if any task did so |
601 |
> |
* @param tasks the tasks |
602 |
> |
* @throws NullPointerException if any task is null |
603 |
|
*/ |
604 |
|
public static void invokeAll(ForkJoinTask<?>... tasks) { |
605 |
|
Throwable ex = null; |
635 |
|
} |
636 |
|
|
637 |
|
/** |
638 |
< |
* Forks all tasks in the collection, returning when |
639 |
< |
* {@code isDone} holds for all of them. If any task |
640 |
< |
* encounters an exception, others may be cancelled. This method |
641 |
< |
* may be invoked only from within ForkJoinTask computations (as |
642 |
< |
* may be determined using method {@link |
643 |
< |
* #inForkJoinPool}). Attempts to invoke in other contexts result |
644 |
< |
* in exceptions or errors, possibly including ClassCastException. |
638 |
> |
* Forks all tasks in the specified collection, returning when |
639 |
> |
* {@code isDone} holds for each task or an (unchecked) exception |
640 |
> |
* is encountered. If any task encounters an exception, others |
641 |
> |
* may be, but are not guaranteed to be, cancelled. If more than |
642 |
> |
* one task encounters an exception, then this method throws any |
643 |
> |
* one of these exceptions. The individual status of each task |
644 |
> |
* may be checked using {@link #getException()} and related |
645 |
> |
* methods. The behavior of this operation is undefined if the |
646 |
> |
* specified collection is modified while the operation is in |
647 |
> |
* progress. |
648 |
> |
* |
649 |
> |
* <p>This method may be invoked only from within {@code |
650 |
> |
* ForkJoinTask} computations (as may be determined using method |
651 |
> |
* {@link #inForkJoinPool}). Attempts to invoke in other contexts |
652 |
> |
* result in exceptions or errors, possibly including {@code |
653 |
> |
* ClassCastException}. |
654 |
|
* |
655 |
|
* @param tasks the collection of tasks |
656 |
|
* @return the tasks argument, to simplify usage |
657 |
|
* @throws NullPointerException if tasks or any element are null |
611 |
– |
* @throws RuntimeException or Error if any task did so |
658 |
|
*/ |
659 |
|
public static <T extends ForkJoinTask<?>> Collection<T> invokeAll(Collection<T> tasks) { |
660 |
< |
if (!(tasks instanceof List<?>)) { |
660 |
> |
if (!(tasks instanceof RandomAccess) || !(tasks instanceof List<?>)) { |
661 |
|
invokeAll(tasks.toArray(new ForkJoinTask<?>[tasks.size()])); |
662 |
|
return tasks; |
663 |
|
} |
698 |
|
} |
699 |
|
|
700 |
|
/** |
701 |
< |
* Returns true if the computation performed by this task has |
702 |
< |
* completed (or has been cancelled). |
701 |
> |
* Attempts to cancel execution of this task. This attempt will |
702 |
> |
* fail if the task has already completed, has already been |
703 |
> |
* cancelled, or could not be cancelled for some other reason. If |
704 |
> |
* successful, and this task has not started when cancel is |
705 |
> |
* called, execution of this task is suppressed, {@link |
706 |
> |
* #isCancelled} will report true, and {@link #join} will result |
707 |
> |
* in a {@code CancellationException} being thrown. |
708 |
> |
* |
709 |
> |
* <p>This method may be overridden in subclasses, but if so, must |
710 |
> |
* still ensure that these minimal properties hold. In particular, |
711 |
> |
* the {@code cancel} method itself must not throw exceptions. |
712 |
> |
* |
713 |
> |
* <p>This method is designed to be invoked by <em>other</em> |
714 |
> |
* tasks. To terminate the current task, you can just return or |
715 |
> |
* throw an unchecked exception from its computation method, or |
716 |
> |
* invoke {@link #completeExceptionally}. |
717 |
> |
* |
718 |
> |
* @param mayInterruptIfRunning this value is ignored in the |
719 |
> |
* default implementation because tasks are not |
720 |
> |
* cancelled via interruption |
721 |
> |
* |
722 |
> |
* @return {@code true} if this task is now cancelled |
723 |
> |
*/ |
724 |
> |
public boolean cancel(boolean mayInterruptIfRunning) { |
725 |
> |
setCompletion(CANCELLED); |
726 |
> |
return (status & COMPLETION_MASK) == CANCELLED; |
727 |
> |
} |
728 |
> |
|
729 |
> |
/** |
730 |
> |
* Returns {@code true} if the computation performed by this task |
731 |
> |
* has completed (or has been cancelled). |
732 |
|
* |
733 |
< |
* @return true if this computation has completed |
733 |
> |
* @return {@code true} if this computation has completed |
734 |
|
*/ |
735 |
|
public final boolean isDone() { |
736 |
|
return status < 0; |
737 |
|
} |
738 |
|
|
739 |
|
/** |
740 |
< |
* Returns true if this task was cancelled. |
740 |
> |
* Returns {@code true} if this task was cancelled. |
741 |
|
* |
742 |
< |
* @return true if this task was cancelled |
742 |
> |
* @return {@code true} if this task was cancelled |
743 |
|
*/ |
744 |
|
public final boolean isCancelled() { |
745 |
|
return (status & COMPLETION_MASK) == CANCELLED; |
746 |
|
} |
747 |
|
|
748 |
|
/** |
749 |
< |
* Asserts that the results of this task's computation will not be |
675 |
< |
* used. If a cancellation occurs before attempting to execute this |
676 |
< |
* task, then execution will be suppressed, {@code isCancelled} |
677 |
< |
* will report true, and {@code join} will result in a |
678 |
< |
* {@code CancellationException} being thrown. Otherwise, when |
679 |
< |
* cancellation races with completion, there are no guarantees |
680 |
< |
* about whether {@code isCancelled} will report true, whether |
681 |
< |
* {@code join} will return normally or via an exception, or |
682 |
< |
* whether these behaviors will remain consistent upon repeated |
683 |
< |
* invocation. |
684 |
< |
* |
685 |
< |
* <p>This method may be overridden in subclasses, but if so, must |
686 |
< |
* still ensure that these minimal properties hold. In particular, |
687 |
< |
* the cancel method itself must not throw exceptions. |
688 |
< |
* |
689 |
< |
* <p> This method is designed to be invoked by <em>other</em> |
690 |
< |
* tasks. To terminate the current task, you can just return or |
691 |
< |
* throw an unchecked exception from its computation method, or |
692 |
< |
* invoke {@code completeExceptionally}. |
749 |
> |
* Returns {@code true} if this task threw an exception or was cancelled. |
750 |
|
* |
751 |
< |
* @param mayInterruptIfRunning this value is ignored in the |
752 |
< |
* default implementation because tasks are not in general |
753 |
< |
* cancelled via interruption |
751 |
> |
* @return {@code true} if this task threw an exception or was cancelled |
752 |
> |
*/ |
753 |
> |
public final boolean isCompletedAbnormally() { |
754 |
> |
return (status & COMPLETION_MASK) < NORMAL; |
755 |
> |
} |
756 |
> |
|
757 |
> |
/** |
758 |
> |
* Returns {@code true} if this task completed without throwing an |
759 |
> |
* exception and was not cancelled. |
760 |
|
* |
761 |
< |
* @return true if this task is now cancelled |
761 |
> |
* @return {@code true} if this task completed without throwing an |
762 |
> |
* exception and was not cancelled |
763 |
|
*/ |
764 |
< |
public boolean cancel(boolean mayInterruptIfRunning) { |
765 |
< |
setCompletion(CANCELLED); |
702 |
< |
return (status & COMPLETION_MASK) == CANCELLED; |
764 |
> |
public final boolean isCompletedNormally() { |
765 |
> |
return (status & COMPLETION_MASK) == NORMAL; |
766 |
|
} |
767 |
|
|
768 |
|
/** |
769 |
< |
* Returns true if this task threw an exception or was cancelled. |
769 |
> |
* Returns {@code true} if this task threw an exception. |
770 |
|
* |
771 |
< |
* @return true if this task threw an exception or was cancelled |
771 |
> |
* @return {@code true} if this task threw an exception |
772 |
|
*/ |
773 |
< |
public final boolean isCompletedAbnormally() { |
774 |
< |
return (status & COMPLETION_MASK) < NORMAL; |
773 |
> |
public final boolean isCompletedExceptionally() { |
774 |
> |
return (status & COMPLETION_MASK) == EXCEPTIONAL; |
775 |
|
} |
776 |
|
|
777 |
|
/** |
778 |
|
* Returns the exception thrown by the base computation, or a |
779 |
< |
* CancellationException if cancelled, or null if none or if the |
780 |
< |
* method has not yet completed. |
779 |
> |
* {@code CancellationException} if cancelled, or {@code null} if |
780 |
> |
* none or if the method has not yet completed. |
781 |
|
* |
782 |
< |
* @return the exception, or null if none |
782 |
> |
* @return the exception, or {@code null} if none |
783 |
|
*/ |
784 |
|
public final Throwable getException() { |
785 |
|
int s = status & COMPLETION_MASK; |
786 |
< |
if (s >= NORMAL) |
787 |
< |
return null; |
788 |
< |
if (s == CANCELLED) |
726 |
< |
return new CancellationException(); |
727 |
< |
return exceptionMap.get(this); |
786 |
> |
return ((s >= NORMAL) ? null : |
787 |
> |
(s == CANCELLED) ? new CancellationException() : |
788 |
> |
exceptionMap.get(this)); |
789 |
|
} |
790 |
|
|
791 |
|
/** |
794 |
|
* {@code join} and related operations. This method may be used |
795 |
|
* to induce exceptions in asynchronous tasks, or to force |
796 |
|
* completion of tasks that would not otherwise complete. Its use |
797 |
< |
* in other situations is likely to be wrong. This method is |
797 |
> |
* in other situations is discouraged. This method is |
798 |
|
* overridable, but overridden versions must invoke {@code super} |
799 |
|
* implementation to maintain guarantees. |
800 |
|
* |
814 |
|
* operations. This method may be used to provide results for |
815 |
|
* asynchronous tasks, or to provide alternative handling for |
816 |
|
* tasks that would not otherwise complete normally. Its use in |
817 |
< |
* other situations is likely to be wrong. This method is |
817 |
> |
* other situations is discouraged. This method is |
818 |
|
* overridable, but overridden versions must invoke {@code super} |
819 |
|
* implementation to maintain guarantees. |
820 |
|
* |
839 |
|
|
840 |
|
public final V get(long timeout, TimeUnit unit) |
841 |
|
throws InterruptedException, ExecutionException, TimeoutException { |
842 |
+ |
long nanos = unit.toNanos(timeout); |
843 |
|
ForkJoinWorkerThread w = getWorker(); |
844 |
|
if (w == null || status < 0 || !w.unpushTask(this) || !tryQuietlyInvoke()) |
845 |
< |
awaitDone(w, unit.toNanos(timeout)); |
845 |
> |
awaitDone(w, nanos); |
846 |
|
return reportTimedFutureResult(); |
847 |
|
} |
848 |
|
|
853 |
|
* there are no potential dependencies between continuation of the |
854 |
|
* current task and that of any other task that might be executed |
855 |
|
* while helping. (This usually holds for pure divide-and-conquer |
856 |
< |
* tasks). This method may be invoked only from within |
857 |
< |
* ForkJoinTask computations (as may be determined using method |
858 |
< |
* {@link #inForkJoinPool}). Attempts to invoke in other contexts |
859 |
< |
* result in exceptions or errors, possibly including |
860 |
< |
* ClassCastException. |
856 |
> |
* tasks). |
857 |
> |
* |
858 |
> |
* <p>This method may be invoked only from within {@code |
859 |
> |
* ForkJoinTask} computations (as may be determined using method |
860 |
> |
* {@link #inForkJoinPool}). Attempts to invoke in other contexts |
861 |
> |
* result in exceptions or errors, possibly including {@code |
862 |
> |
* ClassCastException}. |
863 |
|
* |
864 |
|
* @return the computed result |
865 |
|
*/ |
872 |
|
|
873 |
|
/** |
874 |
|
* Possibly executes other tasks until this task is ready. This |
875 |
< |
* method may be invoked only from within ForkJoinTask |
876 |
< |
* computations (as may be determined using method {@link |
877 |
< |
* #inForkJoinPool}). Attempts to invoke in other contexts result |
878 |
< |
* in exceptions or errors, possibly including ClassCastException. |
875 |
> |
* method may be useful when processing collections of tasks when |
876 |
> |
* some have been cancelled or otherwise known to have aborted. |
877 |
> |
* |
878 |
> |
* <p>This method may be invoked only from within {@code |
879 |
> |
* ForkJoinTask} computations (as may be determined using method |
880 |
> |
* {@link #inForkJoinPool}). Attempts to invoke in other contexts |
881 |
> |
* result in exceptions or errors, possibly including {@code |
882 |
> |
* ClassCastException}. |
883 |
|
*/ |
884 |
|
public final void quietlyHelpJoin() { |
885 |
|
if (status >= 0) { |
918 |
|
|
919 |
|
/** |
920 |
|
* Possibly executes tasks until the pool hosting the current task |
921 |
< |
* {@link ForkJoinPool#isQuiescent}. This method may be of use in |
922 |
< |
* designs in which many tasks are forked, but none are explicitly |
923 |
< |
* joined, instead executing them until all are processed. |
921 |
> |
* {@link ForkJoinPool#isQuiescent is quiescent}. This method may |
922 |
> |
* be of use in designs in which many tasks are forked, but none |
923 |
> |
* are explicitly joined, instead executing them until all are |
924 |
> |
* processed. |
925 |
> |
* |
926 |
> |
* <p>This method may be invoked only from within {@code |
927 |
> |
* ForkJoinTask} computations (as may be determined using method |
928 |
> |
* {@link #inForkJoinPool}). Attempts to invoke in other contexts |
929 |
> |
* result in exceptions or errors, possibly including {@code |
930 |
> |
* ClassCastException}. |
931 |
|
*/ |
932 |
|
public static void helpQuiesce() { |
933 |
|
((ForkJoinWorkerThread) Thread.currentThread()) |
940 |
|
* this task, but only if reuse occurs when this task has either |
941 |
|
* never been forked, or has been forked, then completed and all |
942 |
|
* outstanding joins of this task have also completed. Effects |
943 |
< |
* under any other usage conditions are not guaranteed, and are |
944 |
< |
* almost surely wrong. This method may be useful when executing |
943 |
> |
* under any other usage conditions are not guaranteed. |
944 |
> |
* This method may be useful when executing |
945 |
|
* pre-constructed trees of subtasks in loops. |
946 |
|
*/ |
947 |
|
public void reinitialize() { |
954 |
|
* Returns the pool hosting the current task execution, or null |
955 |
|
* if this task is executing outside of any ForkJoinPool. |
956 |
|
* |
957 |
< |
* @return the pool, or null if none |
957 |
> |
* @see #inForkJoinPool |
958 |
> |
* @return the pool, or {@code null} if none |
959 |
|
*/ |
960 |
|
public static ForkJoinPool getPool() { |
961 |
|
Thread t = Thread.currentThread(); |
980 |
|
* by the current thread, and has not commenced executing in |
981 |
|
* another thread. This method may be useful when arranging |
982 |
|
* alternative local processing of tasks that could have been, but |
983 |
< |
* were not, stolen. This method may be invoked only from within |
908 |
< |
* ForkJoinTask computations (as may be determined using method |
909 |
< |
* {@link #inForkJoinPool}). Attempts to invoke in other contexts |
910 |
< |
* result in exceptions or errors, possibly including |
911 |
< |
* ClassCastException. |
983 |
> |
* were not, stolen. |
984 |
|
* |
985 |
< |
* @return true if unforked |
985 |
> |
* <p>This method may be invoked only from within {@code |
986 |
> |
* ForkJoinTask} computations (as may be determined using method |
987 |
> |
* {@link #inForkJoinPool}). Attempts to invoke in other contexts |
988 |
> |
* result in exceptions or errors, possibly including {@code |
989 |
> |
* ClassCastException}. |
990 |
> |
* |
991 |
> |
* @return {@code true} if unforked |
992 |
|
*/ |
993 |
|
public boolean tryUnfork() { |
994 |
|
return ((ForkJoinWorkerThread) Thread.currentThread()) |
1001 |
|
* value may be useful for heuristic decisions about whether to |
1002 |
|
* fork other tasks. |
1003 |
|
* |
1004 |
+ |
* <p>This method may be invoked only from within {@code |
1005 |
+ |
* ForkJoinTask} computations (as may be determined using method |
1006 |
+ |
* {@link #inForkJoinPool}). Attempts to invoke in other contexts |
1007 |
+ |
* result in exceptions or errors, possibly including {@code |
1008 |
+ |
* ClassCastException}. |
1009 |
+ |
* |
1010 |
|
* @return the number of tasks |
1011 |
|
*/ |
1012 |
|
public static int getQueuedTaskCount() { |
1024 |
|
* tasks, and to process computations locally if this threshold is |
1025 |
|
* exceeded. |
1026 |
|
* |
1027 |
+ |
* <p>This method may be invoked only from within {@code |
1028 |
+ |
* ForkJoinTask} computations (as may be determined using method |
1029 |
+ |
* {@link #inForkJoinPool}). Attempts to invoke in other contexts |
1030 |
+ |
* result in exceptions or errors, possibly including {@code |
1031 |
+ |
* ClassCastException}. |
1032 |
+ |
* |
1033 |
|
* @return the surplus number of tasks, which may be negative |
1034 |
|
*/ |
1035 |
|
public static int getSurplusQueuedTaskCount() { |
1040 |
|
// Extension methods |
1041 |
|
|
1042 |
|
/** |
1043 |
< |
* Returns the result that would be returned by {@code join}, |
1044 |
< |
* even if this task completed abnormally, or null if this task is |
1045 |
< |
* not known to have been completed. This method is designed to |
1046 |
< |
* aid debugging, as well as to support extensions. Its use in any |
1047 |
< |
* other context is discouraged. |
1043 |
> |
* Returns the result that would be returned by {@link #join}, even |
1044 |
> |
* if this task completed abnormally, or {@code null} if this task |
1045 |
> |
* is not known to have been completed. This method is designed |
1046 |
> |
* to aid debugging, as well as to support extensions. Its use in |
1047 |
> |
* any other context is discouraged. |
1048 |
|
* |
1049 |
< |
* @return the result, or null if not completed |
1049 |
> |
* @return the result, or {@code null} if not completed |
1050 |
|
*/ |
1051 |
|
public abstract V getRawResult(); |
1052 |
|
|
1065 |
|
* called otherwise. The return value controls whether this task |
1066 |
|
* is considered to be done normally. It may return false in |
1067 |
|
* asynchronous actions that require explicit invocations of |
1068 |
< |
* {@code complete} to become joinable. It may throw exceptions |
1069 |
< |
* to indicate abnormal exit. |
1068 |
> |
* {@link #complete} to become joinable. It may also throw an |
1069 |
> |
* (unchecked) exception to indicate abnormal exit. |
1070 |
|
* |
1071 |
< |
* @return true if completed normally |
982 |
< |
* @throws Error or RuntimeException if encountered during computation |
1071 |
> |
* @return {@code true} if completed normally |
1072 |
|
*/ |
1073 |
|
protected abstract boolean exec(); |
1074 |
|
|
1075 |
|
/** |
1076 |
< |
* Returns, but does not unschedule or execute, the task queued by |
1077 |
< |
* the current thread but not yet executed, if one is |
1076 |
> |
* Returns, but does not unschedule or execute, a task queued by |
1077 |
> |
* the current thread but not yet executed, if one is immediately |
1078 |
|
* available. There is no guarantee that this task will actually |
1079 |
< |
* be polled or executed next. This method is designed primarily |
1080 |
< |
* to support extensions, and is unlikely to be useful otherwise. |
1081 |
< |
* This method may be invoked only from within ForkJoinTask |
1082 |
< |
* computations (as may be determined using method {@link |
1083 |
< |
* #inForkJoinPool}). Attempts to invoke in other contexts result |
1084 |
< |
* in exceptions or errors, possibly including ClassCastException. |
1079 |
> |
* be polled or executed next. Conversely, this method may return |
1080 |
> |
* null even if a task exists but cannot be accessed without |
1081 |
> |
* contention with other threads. This method is designed |
1082 |
> |
* primarily to support extensions, and is unlikely to be useful |
1083 |
> |
* otherwise. |
1084 |
> |
* |
1085 |
> |
* <p>This method may be invoked only from within {@code |
1086 |
> |
* ForkJoinTask} computations (as may be determined using method |
1087 |
> |
* {@link #inForkJoinPool}). Attempts to invoke in other contexts |
1088 |
> |
* result in exceptions or errors, possibly including {@code |
1089 |
> |
* ClassCastException}. |
1090 |
|
* |
1091 |
< |
* @return the next task, or null if none are available |
1091 |
> |
* @return the next task, or {@code null} if none are available |
1092 |
|
*/ |
1093 |
|
protected static ForkJoinTask<?> peekNextLocalTask() { |
1094 |
|
return ((ForkJoinWorkerThread) Thread.currentThread()) |
1099 |
|
* Unschedules and returns, without executing, the next task |
1100 |
|
* queued by the current thread but not yet executed. This method |
1101 |
|
* is designed primarily to support extensions, and is unlikely to |
1102 |
< |
* be useful otherwise. This method may be invoked only from |
1009 |
< |
* within ForkJoinTask computations (as may be determined using |
1010 |
< |
* method {@link #inForkJoinPool}). Attempts to invoke in other |
1011 |
< |
* contexts result in exceptions or errors, possibly including |
1012 |
< |
* ClassCastException. |
1102 |
> |
* be useful otherwise. |
1103 |
|
* |
1104 |
< |
* @return the next task, or null if none are available |
1104 |
> |
* <p>This method may be invoked only from within {@code |
1105 |
> |
* ForkJoinTask} computations (as may be determined using method |
1106 |
> |
* {@link #inForkJoinPool}). Attempts to invoke in other contexts |
1107 |
> |
* result in exceptions or errors, possibly including {@code |
1108 |
> |
* ClassCastException}. |
1109 |
> |
* |
1110 |
> |
* @return the next task, or {@code null} if none are available |
1111 |
|
*/ |
1112 |
|
protected static ForkJoinTask<?> pollNextLocalTask() { |
1113 |
|
return ((ForkJoinWorkerThread) Thread.currentThread()) |
1122 |
|
* {@code null} result does not necessarily imply quiescence |
1123 |
|
* of the pool this task is operating in. This method is designed |
1124 |
|
* primarily to support extensions, and is unlikely to be useful |
1125 |
< |
* otherwise. This method may be invoked only from within |
1126 |
< |
* ForkJoinTask computations (as may be determined using method |
1127 |
< |
* {@link #inForkJoinPool}). Attempts to invoke in other contexts |
1128 |
< |
* result in exceptions or errors, possibly including |
1129 |
< |
* ClassCastException. |
1125 |
> |
* otherwise. |
1126 |
> |
* |
1127 |
> |
* <p>This method may be invoked only from within {@code |
1128 |
> |
* ForkJoinTask} computations (as may be determined using method |
1129 |
> |
* {@link #inForkJoinPool}). Attempts to invoke in other contexts |
1130 |
> |
* result in exceptions or errors, possibly including {@code |
1131 |
> |
* ClassCastException}. |
1132 |
|
* |
1133 |
< |
* @return a task, or null if none are available |
1133 |
> |
* @return a task, or {@code null} if none are available |
1134 |
|
*/ |
1135 |
|
protected static ForkJoinTask<?> pollTask() { |
1136 |
|
return ((ForkJoinWorkerThread) Thread.currentThread()) |
1137 |
|
.pollTask(); |
1138 |
|
} |
1139 |
|
|
1140 |
< |
// adaptors |
1140 |
> |
/** |
1141 |
> |
* Adaptor for Runnables. This implements RunnableFuture |
1142 |
> |
* to be compliant with AbstractExecutorService constraints |
1143 |
> |
* when used in ForkJoinPool. |
1144 |
> |
*/ |
1145 |
> |
static final class AdaptedRunnable<T> extends ForkJoinTask<T> |
1146 |
> |
implements RunnableFuture<T> { |
1147 |
> |
final Runnable runnable; |
1148 |
> |
final T resultOnCompletion; |
1149 |
> |
T result; |
1150 |
> |
AdaptedRunnable(Runnable runnable, T result) { |
1151 |
> |
if (runnable == null) throw new NullPointerException(); |
1152 |
> |
this.runnable = runnable; |
1153 |
> |
this.resultOnCompletion = result; |
1154 |
> |
} |
1155 |
> |
public T getRawResult() { return result; } |
1156 |
> |
public void setRawResult(T v) { result = v; } |
1157 |
> |
public boolean exec() { |
1158 |
> |
runnable.run(); |
1159 |
> |
result = resultOnCompletion; |
1160 |
> |
return true; |
1161 |
> |
} |
1162 |
> |
public void run() { invoke(); } |
1163 |
> |
private static final long serialVersionUID = 5232453952276885070L; |
1164 |
> |
} |
1165 |
|
|
1166 |
|
/** |
1167 |
< |
* Returns a new ForkJoinTask that performs the {@code run} |
1168 |
< |
* method of the given Runnable as its action, and returns a null |
1169 |
< |
* result upon {@code join}. |
1167 |
> |
* Adaptor for Callables |
1168 |
> |
*/ |
1169 |
> |
static final class AdaptedCallable<T> extends ForkJoinTask<T> |
1170 |
> |
implements RunnableFuture<T> { |
1171 |
> |
final Callable<? extends T> callable; |
1172 |
> |
T result; |
1173 |
> |
AdaptedCallable(Callable<? extends T> callable) { |
1174 |
> |
if (callable == null) throw new NullPointerException(); |
1175 |
> |
this.callable = callable; |
1176 |
> |
} |
1177 |
> |
public T getRawResult() { return result; } |
1178 |
> |
public void setRawResult(T v) { result = v; } |
1179 |
> |
public boolean exec() { |
1180 |
> |
try { |
1181 |
> |
result = callable.call(); |
1182 |
> |
return true; |
1183 |
> |
} catch (Error err) { |
1184 |
> |
throw err; |
1185 |
> |
} catch (RuntimeException rex) { |
1186 |
> |
throw rex; |
1187 |
> |
} catch (Exception ex) { |
1188 |
> |
throw new RuntimeException(ex); |
1189 |
> |
} |
1190 |
> |
} |
1191 |
> |
public void run() { invoke(); } |
1192 |
> |
private static final long serialVersionUID = 2838392045355241008L; |
1193 |
> |
} |
1194 |
> |
|
1195 |
> |
/** |
1196 |
> |
* Returns a new {@code ForkJoinTask} that performs the {@code run} |
1197 |
> |
* method of the given {@code Runnable} as its action, and returns |
1198 |
> |
* a null result upon {@link #join}. |
1199 |
|
* |
1200 |
|
* @param runnable the runnable action |
1201 |
|
* @return the task |
1202 |
|
*/ |
1203 |
< |
public static ForkJoinTask<Void> adapt(Runnable runnable) { |
1204 |
< |
return new ForkJoinPool.AdaptedRunnable<Void>(runnable, null); |
1203 |
> |
public static ForkJoinTask<?> adapt(Runnable runnable) { |
1204 |
> |
return new AdaptedRunnable<Void>(runnable, null); |
1205 |
|
} |
1206 |
|
|
1207 |
|
/** |
1208 |
< |
* Returns a new ForkJoinTask that performs the {@code run} |
1209 |
< |
* method of the given Runnable as its action, and returns the |
1210 |
< |
* given result upon {@code join}. |
1208 |
> |
* Returns a new {@code ForkJoinTask} that performs the {@code run} |
1209 |
> |
* method of the given {@code Runnable} as its action, and returns |
1210 |
> |
* the given result upon {@link #join}. |
1211 |
|
* |
1212 |
|
* @param runnable the runnable action |
1213 |
|
* @param result the result upon completion |
1214 |
|
* @return the task |
1215 |
|
*/ |
1216 |
|
public static <T> ForkJoinTask<T> adapt(Runnable runnable, T result) { |
1217 |
< |
return new ForkJoinPool.AdaptedRunnable<T>(runnable, result); |
1217 |
> |
return new AdaptedRunnable<T>(runnable, result); |
1218 |
|
} |
1219 |
|
|
1220 |
|
/** |
1221 |
< |
* Returns a new ForkJoinTask that performs the {@code call} |
1222 |
< |
* method of the given Callable as its action, and returns its |
1223 |
< |
* result upon {@code join}, translating any checked |
1224 |
< |
* exceptions encountered into {@code RuntimeException}. |
1221 |
> |
* Returns a new {@code ForkJoinTask} that performs the {@code call} |
1222 |
> |
* method of the given {@code Callable} as its action, and returns |
1223 |
> |
* its result upon {@link #join}, translating any checked exceptions |
1224 |
> |
* encountered into {@code RuntimeException}. |
1225 |
|
* |
1226 |
|
* @param callable the callable action |
1227 |
|
* @return the task |
1228 |
|
*/ |
1229 |
< |
public static <T> ForkJoinTask<T> adapt(Callable<T> callable) { |
1230 |
< |
return new ForkJoinPool.AdaptedCallable<T>(callable); |
1229 |
> |
public static <T> ForkJoinTask<T> adapt(Callable<? extends T> callable) { |
1230 |
> |
return new AdaptedCallable<T>(callable); |
1231 |
|
} |
1232 |
|
|
1233 |
|
// Serialization support |
1238 |
|
* Save the state to a stream. |
1239 |
|
* |
1240 |
|
* @serialData the current run status and the exception thrown |
1241 |
< |
* during execution, or null if none |
1241 |
> |
* during execution, or {@code null} if none |
1242 |
|
* @param s the stream |
1243 |
|
*/ |
1244 |
|
private void writeObject(java.io.ObjectOutputStream s) |
1262 |
|
setDoneExceptionally((Throwable) ex); |
1263 |
|
} |
1264 |
|
|
1265 |
< |
// Unsafe mechanics for jsr166y 3rd party package. |
1265 |
> |
// Unsafe mechanics |
1266 |
> |
|
1267 |
> |
private static final sun.misc.Unsafe UNSAFE = getUnsafe(); |
1268 |
> |
private static final long statusOffset = |
1269 |
> |
objectFieldOffset("status", ForkJoinTask.class); |
1270 |
> |
|
1271 |
> |
private static long objectFieldOffset(String field, Class<?> klazz) { |
1272 |
> |
try { |
1273 |
> |
return UNSAFE.objectFieldOffset(klazz.getDeclaredField(field)); |
1274 |
> |
} catch (NoSuchFieldException e) { |
1275 |
> |
// Convert Exception to corresponding Error |
1276 |
> |
NoSuchFieldError error = new NoSuchFieldError(field); |
1277 |
> |
error.initCause(e); |
1278 |
> |
throw error; |
1279 |
> |
} |
1280 |
> |
} |
1281 |
> |
|
1282 |
> |
/** |
1283 |
> |
* Returns a sun.misc.Unsafe. Suitable for use in a 3rd party package. |
1284 |
> |
* Replace with a simple call to Unsafe.getUnsafe when integrating |
1285 |
> |
* into a jdk. |
1286 |
> |
* |
1287 |
> |
* @return a sun.misc.Unsafe |
1288 |
> |
*/ |
1289 |
|
private static sun.misc.Unsafe getUnsafe() { |
1290 |
|
try { |
1291 |
|
return sun.misc.Unsafe.getUnsafe(); |
1292 |
|
} catch (SecurityException se) { |
1293 |
|
try { |
1294 |
|
return java.security.AccessController.doPrivileged |
1295 |
< |
(new java.security.PrivilegedExceptionAction<sun.misc.Unsafe>() { |
1295 |
> |
(new java.security |
1296 |
> |
.PrivilegedExceptionAction<sun.misc.Unsafe>() { |
1297 |
|
public sun.misc.Unsafe run() throws Exception { |
1298 |
< |
return getUnsafeByReflection(); |
1298 |
> |
java.lang.reflect.Field f = sun.misc |
1299 |
> |
.Unsafe.class.getDeclaredField("theUnsafe"); |
1300 |
> |
f.setAccessible(true); |
1301 |
> |
return (sun.misc.Unsafe) f.get(null); |
1302 |
|
}}); |
1303 |
|
} catch (java.security.PrivilegedActionException e) { |
1304 |
|
throw new RuntimeException("Could not initialize intrinsics", |
1306 |
|
} |
1307 |
|
} |
1308 |
|
} |
1131 |
– |
|
1132 |
– |
private static sun.misc.Unsafe getUnsafeByReflection() |
1133 |
– |
throws NoSuchFieldException, IllegalAccessException { |
1134 |
– |
java.lang.reflect.Field f = |
1135 |
– |
sun.misc.Unsafe.class.getDeclaredField("theUnsafe"); |
1136 |
– |
f.setAccessible(true); |
1137 |
– |
return (sun.misc.Unsafe) f.get(null); |
1138 |
– |
} |
1139 |
– |
|
1140 |
– |
private static long fieldOffset(String fieldName, Class<?> klazz) { |
1141 |
– |
try { |
1142 |
– |
return UNSAFE.objectFieldOffset(klazz.getDeclaredField(fieldName)); |
1143 |
– |
} catch (NoSuchFieldException e) { |
1144 |
– |
// Convert Exception to Error |
1145 |
– |
NoSuchFieldError error = new NoSuchFieldError(fieldName); |
1146 |
– |
error.initCause(e); |
1147 |
– |
throw error; |
1148 |
– |
} |
1149 |
– |
} |
1150 |
– |
|
1151 |
– |
private static final sun.misc.Unsafe UNSAFE = getUnsafe(); |
1152 |
– |
private static final long statusOffset = |
1153 |
– |
fieldOffset("status", ForkJoinTask.class); |
1154 |
– |
|
1309 |
|
} |