16 |
|
import java.util.WeakHashMap; |
17 |
|
|
18 |
|
/** |
19 |
< |
* Abstract base class for tasks that run within a {@link |
20 |
< |
* ForkJoinPool}. A ForkJoinTask is a thread-like entity that is much |
19 |
> |
* Abstract base class for tasks that run within a {@link ForkJoinPool}. |
20 |
> |
* A {@code ForkJoinTask} is a thread-like entity that is much |
21 |
|
* lighter weight than a normal thread. Huge numbers of tasks and |
22 |
|
* subtasks may be hosted by a small number of actual threads in a |
23 |
|
* ForkJoinPool, at the price of some usage limitations. |
24 |
|
* |
25 |
< |
* <p> A "main" ForkJoinTask begins execution when submitted to a |
26 |
< |
* {@link ForkJoinPool}. Once started, it will usually in turn start |
27 |
< |
* other subtasks. As indicated by the name of this class, many |
28 |
< |
* programs using ForkJoinTasks employ only methods {@code fork} |
29 |
< |
* and {@code join}, or derivatives such as |
30 |
< |
* {@code invokeAll}. However, this class also provides a number |
31 |
< |
* of other methods that can come into play in advanced usages, as |
32 |
< |
* well as extension mechanics that allow support of new forms of |
33 |
< |
* fork/join processing. |
25 |
> |
* <p>A "main" {@code ForkJoinTask} begins execution when submitted |
26 |
> |
* to a {@link ForkJoinPool}. Once started, it will usually in turn |
27 |
> |
* start other subtasks. As indicated by the name of this class, |
28 |
> |
* many programs using {@code ForkJoinTask} employ only methods |
29 |
> |
* {@link #fork} and {@link #join}, or derivatives such as {@link |
30 |
> |
* #invokeAll}. However, this class also provides a number of other |
31 |
> |
* methods that can come into play in advanced usages, as well as |
32 |
> |
* extension mechanics that allow support of new forms of fork/join |
33 |
> |
* processing. |
34 |
|
* |
35 |
< |
* <p>A ForkJoinTask is a lightweight form of {@link Future}. The |
36 |
< |
* efficiency of ForkJoinTasks stems from a set of restrictions (that |
37 |
< |
* are only partially statically enforceable) reflecting their |
38 |
< |
* intended use as computational tasks calculating pure functions or |
39 |
< |
* operating on purely isolated objects. The primary coordination |
40 |
< |
* mechanisms are {@link #fork}, that arranges asynchronous execution, |
41 |
< |
* and {@link #join}, that doesn't proceed until the task's result has |
42 |
< |
* been computed. Computations should avoid {@code synchronized} |
43 |
< |
* methods or blocks, and should minimize other blocking |
44 |
< |
* synchronization apart from joining other tasks or using |
45 |
< |
* synchronizers such as Phasers that are advertised to cooperate with |
46 |
< |
* fork/join scheduling. Tasks should also not perform blocking IO, |
47 |
< |
* and should ideally access variables that are completely independent |
48 |
< |
* of those accessed by other running tasks. Minor breaches of these |
49 |
< |
* restrictions, for example using shared output streams, may be |
50 |
< |
* tolerable in practice, but frequent use may result in poor |
51 |
< |
* performance, and the potential to indefinitely stall if the number |
52 |
< |
* of threads not waiting for IO or other external synchronization |
53 |
< |
* becomes exhausted. This usage restriction is in part enforced by |
54 |
< |
* not permitting checked exceptions such as {@code IOExceptions} |
55 |
< |
* to be thrown. However, computations may still encounter unchecked |
56 |
< |
* exceptions, that are rethrown to callers attempting join |
57 |
< |
* them. These exceptions may additionally include |
58 |
< |
* RejectedExecutionExceptions stemming from internal resource |
59 |
< |
* exhaustion such as failure to allocate internal task queues. |
35 |
> |
* <p>A {@code ForkJoinTask} is a lightweight form of {@link Future}. |
36 |
> |
* The efficiency of {@code ForkJoinTask}s stems from a set of |
37 |
> |
* restrictions (that are only partially statically enforceable) |
38 |
> |
* reflecting their intended use as computational tasks calculating |
39 |
> |
* pure functions or operating on purely isolated objects. The |
40 |
> |
* primary coordination mechanisms are {@link #fork}, that arranges |
41 |
> |
* asynchronous execution, and {@link #join}, that doesn't proceed |
42 |
> |
* until the task's result has been computed. Computations should |
43 |
> |
* avoid {@code synchronized} methods or blocks, and should minimize |
44 |
> |
* other blocking synchronization apart from joining other tasks or |
45 |
> |
* using synchronizers such as Phasers that are advertised to |
46 |
> |
* cooperate with fork/join scheduling. Tasks should also not perform |
47 |
> |
* blocking IO, and should ideally access variables that are |
48 |
> |
* completely independent of those accessed by other running |
49 |
> |
* tasks. Minor breaches of these restrictions, for example using |
50 |
> |
* shared output streams, may be tolerable in practice, but frequent |
51 |
> |
* use may result in poor performance, and the potential to |
52 |
> |
* indefinitely stall if the number of threads not waiting for IO or |
53 |
> |
* other external synchronization becomes exhausted. This usage |
54 |
> |
* restriction is in part enforced by not permitting checked |
55 |
> |
* exceptions such as {@code IOExceptions} to be thrown. However, |
56 |
> |
* computations may still encounter unchecked exceptions, that are |
57 |
> |
* rethrown to callers attempting join them. These exceptions may |
58 |
> |
* additionally include RejectedExecutionExceptions stemming from |
59 |
> |
* internal resource exhaustion such as failure to allocate internal |
60 |
> |
* task queues. |
61 |
|
* |
62 |
|
* <p>The primary method for awaiting completion and extracting |
63 |
|
* results of a task is {@link #join}, but there are several variants: |
76 |
|
* performs the most common form of parallel invocation: forking a set |
77 |
|
* of tasks and joining them all. |
78 |
|
* |
79 |
< |
* <p> The ForkJoinTask class is not usually directly subclassed. |
79 |
> |
* <p>The ForkJoinTask class is not usually directly subclassed. |
80 |
|
* Instead, you subclass one of the abstract classes that support a |
81 |
< |
* particular style of fork/join processing. Normally, a concrete |
81 |
> |
* particular style of fork/join processing, typically {@link |
82 |
> |
* RecursiveAction} for computations that do not return results, or |
83 |
> |
* {@link RecursiveTask} for those that do. Normally, a concrete |
84 |
|
* ForkJoinTask subclass declares fields comprising its parameters, |
85 |
|
* established in a constructor, and then defines a {@code compute} |
86 |
|
* method that somehow uses the control methods supplied by this base |
97 |
|
* lightweight task scheduling framework, and so cannot be overridden. |
98 |
|
* Developers creating new basic styles of fork/join processing should |
99 |
|
* minimally implement {@code protected} methods |
100 |
< |
* {@code exec}, {@code setRawResult}, and |
101 |
< |
* {@code getRawResult}, while also introducing an abstract |
100 |
> |
* {@link #exec}, {@link #setRawResult}, and |
101 |
> |
* {@link #getRawResult}, while also introducing an abstract |
102 |
|
* computational method that can be implemented in its subclasses, |
103 |
|
* possibly relying on other {@code protected} methods provided |
104 |
|
* by this class. |
110 |
|
* parallelism cannot improve throughput. If too small, then memory |
111 |
|
* and internal task maintenance overhead may overwhelm processing. |
112 |
|
* |
113 |
+ |
* <p>This class provides {@code adapt} methods for {@link |
114 |
+ |
* java.lang.Runnable} and {@link java.util.concurrent.Callable}, that |
115 |
+ |
* may be of use when mixing execution of ForkJoinTasks with other |
116 |
+ |
* kinds of tasks. When all tasks are of this form, consider using a |
117 |
+ |
* pool in {@link ForkJoinPool#setAsyncMode}. |
118 |
+ |
* |
119 |
|
* <p>ForkJoinTasks are {@code Serializable}, which enables them |
120 |
|
* to be used in extensions such as remote execution frameworks. It is |
121 |
|
* in general sensible to serialize tasks only before or after, but |
257 |
|
synchronized (this) { |
258 |
|
try { |
259 |
|
while (status >= 0) { |
260 |
< |
long nt = nanos - System.nanoTime() - startTime; |
260 |
> |
long nt = nanos - (System.nanoTime() - startTime); |
261 |
|
if (nt <= 0) |
262 |
|
break; |
263 |
|
wait(nt / 1000000, (int) (nt % 1000000)); |
502 |
|
* #inForkJoinPool}). Attempts to invoke in other contexts result |
503 |
|
* in exceptions or errors, possibly including ClassCastException. |
504 |
|
* |
505 |
< |
* @return <code>this</code>, to simplify usage. |
505 |
> |
* @return {@code this}, to simplify usage. |
506 |
|
*/ |
507 |
|
public final ForkJoinTask<V> fork() { |
508 |
|
((ForkJoinWorkerThread) Thread.currentThread()) |
541 |
|
} |
542 |
|
|
543 |
|
/** |
544 |
< |
* Forks both tasks, returning when {@code isDone} holds for |
545 |
< |
* both of them or an exception is encountered. This method may be |
544 |
> |
* Forks the given tasks, returning when {@code isDone} holds for |
545 |
> |
* each task or an exception is encountered. This method may be |
546 |
|
* invoked only from within ForkJoinTask computations (as may be |
547 |
|
* determined using method {@link #inForkJoinPool}). Attempts to |
548 |
|
* invoke in other contexts result in exceptions or errors, |
549 |
|
* possibly including ClassCastException. |
550 |
|
* |
551 |
< |
* @param t1 one task |
552 |
< |
* @param t2 the other task |
553 |
< |
* @throws NullPointerException if t1 or t2 are null |
554 |
< |
* @throws RuntimeException or Error if either task did so |
551 |
> |
* @param t1 the first task |
552 |
> |
* @param t2 the second task |
553 |
> |
* @throws NullPointerException if any task is null |
554 |
> |
* @throws RuntimeException or Error if a task did so |
555 |
|
*/ |
556 |
|
public static void invokeAll(ForkJoinTask<?>t1, ForkJoinTask<?> t2) { |
557 |
|
t2.fork(); |
560 |
|
} |
561 |
|
|
562 |
|
/** |
563 |
< |
* Forks the given tasks, returning when {@code isDone} holds |
564 |
< |
* for all of them. If any task encounters an exception, others |
565 |
< |
* may be cancelled. This method may be invoked only from within |
563 |
> |
* Forks the given tasks, returning when {@code isDone} holds for |
564 |
> |
* each task or an exception is encountered. If any task |
565 |
> |
* encounters an exception, others may be, but are not guaranteed |
566 |
> |
* to be, cancelled. This method may be invoked only from within |
567 |
|
* ForkJoinTask computations (as may be determined using method |
568 |
|
* {@link #inForkJoinPool}). Attempts to invoke in other contexts |
569 |
|
* result in exceptions or errors, possibly including |
570 |
|
* ClassCastException. |
571 |
|
* |
572 |
< |
* @param tasks the array of tasks |
572 |
> |
* Overloadings of this method exist for the special cases |
573 |
> |
* of one to four arguments. |
574 |
> |
* |
575 |
> |
* @param tasks the tasks |
576 |
|
* @throws NullPointerException if tasks or any element are null |
577 |
|
* @throws RuntimeException or Error if any task did so |
578 |
|
*/ |
610 |
|
} |
611 |
|
|
612 |
|
/** |
613 |
< |
* Forks all tasks in the collection, returning when |
614 |
< |
* {@code isDone} holds for all of them. If any task |
615 |
< |
* encounters an exception, others may be cancelled. This method |
616 |
< |
* may be invoked only from within ForkJoinTask computations (as |
617 |
< |
* may be determined using method {@link |
618 |
< |
* #inForkJoinPool}). Attempts to invoke in other contexts result |
619 |
< |
* in exceptions or errors, possibly including ClassCastException. |
613 |
> |
* Forks all tasks in the collection, returning when {@code |
614 |
> |
* isDone} holds for each task or an exception is encountered. If |
615 |
> |
* any task encounters an exception, others may be, but are not |
616 |
> |
* guaranteed to be, cancelled. This method may be invoked only |
617 |
> |
* from within ForkJoinTask computations (as may be determined |
618 |
> |
* using method {@link #inForkJoinPool}). Attempts to invoke in |
619 |
> |
* other contexts result in exceptions or errors, possibly |
620 |
> |
* including ClassCastException. |
621 |
|
* |
622 |
|
* @param tasks the collection of tasks |
623 |
|
* @return the tasks argument, to simplify usage |
666 |
|
} |
667 |
|
|
668 |
|
/** |
669 |
< |
* Returns true if the computation performed by this task has |
670 |
< |
* completed (or has been cancelled). |
669 |
> |
* Returns {@code true} if the computation performed by this task |
670 |
> |
* has completed (or has been cancelled). |
671 |
|
* |
672 |
< |
* @return true if this computation has completed |
672 |
> |
* @return {@code true} if this computation has completed |
673 |
|
*/ |
674 |
|
public final boolean isDone() { |
675 |
|
return status < 0; |
676 |
|
} |
677 |
|
|
678 |
|
/** |
679 |
< |
* Returns true if this task was cancelled. |
679 |
> |
* Returns {@code true} if this task was cancelled. |
680 |
|
* |
681 |
< |
* @return true if this task was cancelled |
681 |
> |
* @return {@code true} if this task was cancelled |
682 |
|
*/ |
683 |
|
public final boolean isCancelled() { |
684 |
|
return (status & COMPLETION_MASK) == CANCELLED; |
687 |
|
/** |
688 |
|
* Asserts that the results of this task's computation will not be |
689 |
|
* used. If a cancellation occurs before attempting to execute this |
690 |
< |
* task, then execution will be suppressed, {@code isCancelled} |
691 |
< |
* will report true, and {@code join} will result in a |
690 |
> |
* task, execution will be suppressed, {@link #isCancelled} |
691 |
> |
* will report true, and {@link #join} will result in a |
692 |
|
* {@code CancellationException} being thrown. Otherwise, when |
693 |
|
* cancellation races with completion, there are no guarantees |
694 |
< |
* about whether {@code isCancelled} will report true, whether |
695 |
< |
* {@code join} will return normally or via an exception, or |
696 |
< |
* whether these behaviors will remain consistent upon repeated |
694 |
> |
* about whether {@code isCancelled} will report {@code true}, |
695 |
> |
* whether {@code join} will return normally or via an exception, |
696 |
> |
* or whether these behaviors will remain consistent upon repeated |
697 |
|
* invocation. |
698 |
|
* |
699 |
|
* <p>This method may be overridden in subclasses, but if so, must |
700 |
|
* still ensure that these minimal properties hold. In particular, |
701 |
< |
* the cancel method itself must not throw exceptions. |
701 |
> |
* the {@code cancel} method itself must not throw exceptions. |
702 |
|
* |
703 |
< |
* <p> This method is designed to be invoked by <em>other</em> |
703 |
> |
* <p>This method is designed to be invoked by <em>other</em> |
704 |
|
* tasks. To terminate the current task, you can just return or |
705 |
|
* throw an unchecked exception from its computation method, or |
706 |
< |
* invoke {@code completeExceptionally}. |
706 |
> |
* invoke {@link #completeExceptionally}. |
707 |
|
* |
708 |
|
* @param mayInterruptIfRunning this value is ignored in the |
709 |
|
* default implementation because tasks are not in general |
710 |
|
* cancelled via interruption |
711 |
|
* |
712 |
< |
* @return true if this task is now cancelled |
712 |
> |
* @return {@code true} if this task is now cancelled |
713 |
|
*/ |
714 |
|
public boolean cancel(boolean mayInterruptIfRunning) { |
715 |
|
setCompletion(CANCELLED); |
717 |
|
} |
718 |
|
|
719 |
|
/** |
720 |
< |
* Returns true if this task threw an exception or was cancelled. |
720 |
> |
* Returns {@code true} if this task threw an exception or was cancelled. |
721 |
|
* |
722 |
< |
* @return true if this task threw an exception or was cancelled |
722 |
> |
* @return {@code true} if this task threw an exception or was cancelled |
723 |
|
*/ |
724 |
|
public final boolean isCompletedAbnormally() { |
725 |
|
return (status & COMPLETION_MASK) < NORMAL; |
727 |
|
|
728 |
|
/** |
729 |
|
* Returns the exception thrown by the base computation, or a |
730 |
< |
* CancellationException if cancelled, or null if none or if the |
731 |
< |
* method has not yet completed. |
730 |
> |
* {@code CancellationException} if cancelled, or {@code null} if |
731 |
> |
* none or if the method has not yet completed. |
732 |
|
* |
733 |
< |
* @return the exception, or null if none |
733 |
> |
* @return the exception, or {@code null} if none |
734 |
|
*/ |
735 |
|
public final Throwable getException() { |
736 |
|
int s = status & COMPLETION_MASK; |
747 |
|
* {@code join} and related operations. This method may be used |
748 |
|
* to induce exceptions in asynchronous tasks, or to force |
749 |
|
* completion of tasks that would not otherwise complete. Its use |
750 |
< |
* in other situations is likely to be wrong. This method is |
750 |
> |
* in other situations is discouraged. This method is |
751 |
|
* overridable, but overridden versions must invoke {@code super} |
752 |
|
* implementation to maintain guarantees. |
753 |
|
* |
767 |
|
* operations. This method may be used to provide results for |
768 |
|
* asynchronous tasks, or to provide alternative handling for |
769 |
|
* tasks that would not otherwise complete normally. Its use in |
770 |
< |
* other situations is likely to be wrong. This method is |
770 |
> |
* other situations is discouraged. This method is |
771 |
|
* overridable, but overridden versions must invoke {@code super} |
772 |
|
* implementation to maintain guarantees. |
773 |
|
* |
792 |
|
|
793 |
|
public final V get(long timeout, TimeUnit unit) |
794 |
|
throws InterruptedException, ExecutionException, TimeoutException { |
795 |
+ |
long nanos = unit.toNanos(timeout); |
796 |
|
ForkJoinWorkerThread w = getWorker(); |
797 |
|
if (w == null || status < 0 || !w.unpushTask(this) || !tryQuietlyInvoke()) |
798 |
< |
awaitDone(w, unit.toNanos(timeout)); |
798 |
> |
awaitDone(w, nanos); |
799 |
|
return reportTimedFutureResult(); |
800 |
|
} |
801 |
|
|
867 |
|
* Possibly executes tasks until the pool hosting the current task |
868 |
|
* {@link ForkJoinPool#isQuiescent}. This method may be of use in |
869 |
|
* designs in which many tasks are forked, but none are explicitly |
870 |
< |
* joined, instead executing them until all are processed. |
870 |
> |
* joined, instead executing them until all are processed. This |
871 |
> |
* method may be invoked only from within ForkJoinTask |
872 |
> |
* computations (as may be determined using method {@link |
873 |
> |
* #inForkJoinPool}). Attempts to invoke in other contexts result |
874 |
> |
* in exceptions or errors, possibly including ClassCastException. |
875 |
|
*/ |
876 |
|
public static void helpQuiesce() { |
877 |
|
((ForkJoinWorkerThread) Thread.currentThread()) |
884 |
|
* this task, but only if reuse occurs when this task has either |
885 |
|
* never been forked, or has been forked, then completed and all |
886 |
|
* outstanding joins of this task have also completed. Effects |
887 |
< |
* under any other usage conditions are not guaranteed, and are |
888 |
< |
* almost surely wrong. This method may be useful when executing |
887 |
> |
* under any other usage conditions are not guaranteed. |
888 |
> |
* This method may be useful when executing |
889 |
|
* pre-constructed trees of subtasks in loops. |
890 |
|
*/ |
891 |
|
public void reinitialize() { |
898 |
|
* Returns the pool hosting the current task execution, or null |
899 |
|
* if this task is executing outside of any ForkJoinPool. |
900 |
|
* |
901 |
< |
* @return the pool, or null if none |
901 |
> |
* @see #inForkJoinPool |
902 |
> |
* @return the pool, or {@code null} if none |
903 |
|
*/ |
904 |
|
public static ForkJoinPool getPool() { |
905 |
|
Thread t = Thread.currentThread(); |
930 |
|
* result in exceptions or errors, possibly including |
931 |
|
* ClassCastException. |
932 |
|
* |
933 |
< |
* @return true if unforked |
933 |
> |
* @return {@code true} if unforked |
934 |
|
*/ |
935 |
|
public boolean tryUnfork() { |
936 |
|
return ((ForkJoinWorkerThread) Thread.currentThread()) |
941 |
|
* Returns an estimate of the number of tasks that have been |
942 |
|
* forked by the current worker thread but not yet executed. This |
943 |
|
* value may be useful for heuristic decisions about whether to |
944 |
< |
* fork other tasks. |
945 |
< |
* |
944 |
> |
* fork other tasks. This method may be invoked only from within |
945 |
> |
* ForkJoinTask computations (as may be determined using method |
946 |
> |
* {@link #inForkJoinPool}). Attempts to invoke in other contexts |
947 |
> |
* result in exceptions or errors, possibly including |
948 |
> |
* ClassCastException. |
949 |
|
* @return the number of tasks |
950 |
|
*/ |
951 |
|
public static int getQueuedTaskCount() { |
961 |
|
* usages of ForkJoinTasks, at steady state, each worker should |
962 |
|
* aim to maintain a small constant surplus (for example, 3) of |
963 |
|
* tasks, and to process computations locally if this threshold is |
964 |
< |
* exceeded. |
965 |
< |
* |
964 |
> |
* exceeded. This method may be invoked only from within |
965 |
> |
* ForkJoinTask computations (as may be determined using method |
966 |
> |
* {@link #inForkJoinPool}). Attempts to invoke in other contexts |
967 |
> |
* result in exceptions or errors, possibly including |
968 |
> |
* ClassCastException. |
969 |
|
* @return the surplus number of tasks, which may be negative |
970 |
|
*/ |
971 |
|
public static int getSurplusQueuedTaskCount() { |
976 |
|
// Extension methods |
977 |
|
|
978 |
|
/** |
979 |
< |
* Returns the result that would be returned by {@code join}, |
980 |
< |
* even if this task completed abnormally, or null if this task is |
981 |
< |
* not known to have been completed. This method is designed to |
982 |
< |
* aid debugging, as well as to support extensions. Its use in any |
983 |
< |
* other context is discouraged. |
979 |
> |
* Returns the result that would be returned by {@link #join}, even |
980 |
> |
* if this task completed abnormally, or {@code null} if this task |
981 |
> |
* is not known to have been completed. This method is designed |
982 |
> |
* to aid debugging, as well as to support extensions. Its use in |
983 |
> |
* any other context is discouraged. |
984 |
|
* |
985 |
< |
* @return the result, or null if not completed |
985 |
> |
* @return the result, or {@code null} if not completed |
986 |
|
*/ |
987 |
|
public abstract V getRawResult(); |
988 |
|
|
1001 |
|
* called otherwise. The return value controls whether this task |
1002 |
|
* is considered to be done normally. It may return false in |
1003 |
|
* asynchronous actions that require explicit invocations of |
1004 |
< |
* {@code complete} to become joinable. It may throw exceptions |
1004 |
> |
* {@link #complete} to become joinable. It may throw exceptions |
1005 |
|
* to indicate abnormal exit. |
1006 |
|
* |
1007 |
< |
* @return true if completed normally |
1007 |
> |
* @return {@code true} if completed normally |
1008 |
|
* @throws Error or RuntimeException if encountered during computation |
1009 |
|
*/ |
1010 |
|
protected abstract boolean exec(); |
1011 |
|
|
1012 |
|
/** |
1013 |
< |
* Returns, but does not unschedule or execute, the task queued by |
1014 |
< |
* the current thread but not yet executed, if one is |
1013 |
> |
* Returns, but does not unschedule or execute, a task queued by |
1014 |
> |
* the current thread but not yet executed, if one is immediately |
1015 |
|
* available. There is no guarantee that this task will actually |
1016 |
< |
* be polled or executed next. This method is designed primarily |
1017 |
< |
* to support extensions, and is unlikely to be useful otherwise. |
1018 |
< |
* This method may be invoked only from within ForkJoinTask |
1019 |
< |
* computations (as may be determined using method {@link |
1020 |
< |
* #inForkJoinPool}). Attempts to invoke in other contexts result |
1021 |
< |
* in exceptions or errors, possibly including ClassCastException. |
1016 |
> |
* be polled or executed next. Conversely, this method may return |
1017 |
> |
* null even if a task exists but cannot be accessed without |
1018 |
> |
* contention with other threads. This method is designed |
1019 |
> |
* primarily to support extensions, and is unlikely to be useful |
1020 |
> |
* otherwise. This method may be invoked only from within |
1021 |
> |
* ForkJoinTask computations (as may be determined using method |
1022 |
> |
* {@link #inForkJoinPool}). Attempts to invoke in other contexts |
1023 |
> |
* result in exceptions or errors, possibly including |
1024 |
> |
* ClassCastException. |
1025 |
|
* |
1026 |
< |
* @return the next task, or null if none are available |
1026 |
> |
* @return the next task, or {@code null} if none are available |
1027 |
|
*/ |
1028 |
|
protected static ForkJoinTask<?> peekNextLocalTask() { |
1029 |
|
return ((ForkJoinWorkerThread) Thread.currentThread()) |
1040 |
|
* contexts result in exceptions or errors, possibly including |
1041 |
|
* ClassCastException. |
1042 |
|
* |
1043 |
< |
* @return the next task, or null if none are available |
1043 |
> |
* @return the next task, or {@code null} if none are available |
1044 |
|
*/ |
1045 |
|
protected static ForkJoinTask<?> pollNextLocalTask() { |
1046 |
|
return ((ForkJoinWorkerThread) Thread.currentThread()) |
1061 |
|
* result in exceptions or errors, possibly including |
1062 |
|
* ClassCastException. |
1063 |
|
* |
1064 |
< |
* @return a task, or null if none are available |
1064 |
> |
* @return a task, or {@code null} if none are available |
1065 |
|
*/ |
1066 |
|
protected static ForkJoinTask<?> pollTask() { |
1067 |
|
return ((ForkJoinWorkerThread) Thread.currentThread()) |
1068 |
|
.pollTask(); |
1069 |
|
} |
1070 |
|
|
1071 |
< |
// adaptors |
1071 |
> |
/** |
1072 |
> |
* Adaptor for Runnables. This implements RunnableFuture |
1073 |
> |
* to be compliant with AbstractExecutorService constraints |
1074 |
> |
* when used in ForkJoinPool. |
1075 |
> |
*/ |
1076 |
> |
static final class AdaptedRunnable<T> extends ForkJoinTask<T> |
1077 |
> |
implements RunnableFuture<T> { |
1078 |
> |
final Runnable runnable; |
1079 |
> |
final T resultOnCompletion; |
1080 |
> |
T result; |
1081 |
> |
AdaptedRunnable(Runnable runnable, T result) { |
1082 |
> |
if (runnable == null) throw new NullPointerException(); |
1083 |
> |
this.runnable = runnable; |
1084 |
> |
this.resultOnCompletion = result; |
1085 |
> |
} |
1086 |
> |
public T getRawResult() { return result; } |
1087 |
> |
public void setRawResult(T v) { result = v; } |
1088 |
> |
public boolean exec() { |
1089 |
> |
runnable.run(); |
1090 |
> |
result = resultOnCompletion; |
1091 |
> |
return true; |
1092 |
> |
} |
1093 |
> |
public void run() { invoke(); } |
1094 |
> |
private static final long serialVersionUID = 5232453952276885070L; |
1095 |
> |
} |
1096 |
|
|
1097 |
|
/** |
1098 |
< |
* Returns a new ForkJoinTask that performs the <code>run</code> |
1098 |
> |
* Adaptor for Callables |
1099 |
> |
*/ |
1100 |
> |
static final class AdaptedCallable<T> extends ForkJoinTask<T> |
1101 |
> |
implements RunnableFuture<T> { |
1102 |
> |
final Callable<? extends T> callable; |
1103 |
> |
T result; |
1104 |
> |
AdaptedCallable(Callable<? extends T> callable) { |
1105 |
> |
if (callable == null) throw new NullPointerException(); |
1106 |
> |
this.callable = callable; |
1107 |
> |
} |
1108 |
> |
public T getRawResult() { return result; } |
1109 |
> |
public void setRawResult(T v) { result = v; } |
1110 |
> |
public boolean exec() { |
1111 |
> |
try { |
1112 |
> |
result = callable.call(); |
1113 |
> |
return true; |
1114 |
> |
} catch (Error err) { |
1115 |
> |
throw err; |
1116 |
> |
} catch (RuntimeException rex) { |
1117 |
> |
throw rex; |
1118 |
> |
} catch (Exception ex) { |
1119 |
> |
throw new RuntimeException(ex); |
1120 |
> |
} |
1121 |
> |
} |
1122 |
> |
public void run() { invoke(); } |
1123 |
> |
private static final long serialVersionUID = 2838392045355241008L; |
1124 |
> |
} |
1125 |
> |
|
1126 |
> |
/** |
1127 |
> |
* Returns a new ForkJoinTask that performs the {@code run} |
1128 |
|
* method of the given Runnable as its action, and returns a null |
1129 |
< |
* result upon <code>join</code>. |
1129 |
> |
* result upon {@code join}. |
1130 |
|
* |
1131 |
|
* @param runnable the runnable action |
1132 |
|
* @return the task |
1133 |
|
*/ |
1134 |
< |
public static ForkJoinTask<Void> adapt(Runnable runnable) { |
1135 |
< |
return new ForkJoinPool.AdaptedRunnable<Void>(runnable, null); |
1134 |
> |
public static ForkJoinTask<?> adapt(Runnable runnable) { |
1135 |
> |
return new AdaptedRunnable<Void>(runnable, null); |
1136 |
|
} |
1137 |
|
|
1138 |
|
/** |
1139 |
< |
* Returns a new ForkJoinTask that performs the <code>run</code> |
1139 |
> |
* Returns a new ForkJoinTask that performs the {@code run} |
1140 |
|
* method of the given Runnable as its action, and returns the |
1141 |
< |
* given result upon <code>join</code>. |
1141 |
> |
* given result upon {@code join}. |
1142 |
|
* |
1143 |
|
* @param runnable the runnable action |
1144 |
|
* @param result the result upon completion |
1145 |
|
* @return the task |
1146 |
|
*/ |
1147 |
|
public static <T> ForkJoinTask<T> adapt(Runnable runnable, T result) { |
1148 |
< |
return new ForkJoinPool.AdaptedRunnable<T>(runnable, result); |
1148 |
> |
return new AdaptedRunnable<T>(runnable, result); |
1149 |
|
} |
1150 |
|
|
1151 |
|
/** |
1152 |
< |
* Returns a new ForkJoinTask that performs the <code>call</code> |
1152 |
> |
* Returns a new ForkJoinTask that performs the {@code call} |
1153 |
|
* method of the given Callable as its action, and returns its |
1154 |
< |
* result upon <code>join</code>, translating any checked |
1155 |
< |
* exceptions encountered into <code>RuntimeException<code>. |
1154 |
> |
* result upon {@code join}, translating any checked |
1155 |
> |
* exceptions encountered into {@code RuntimeException}. |
1156 |
|
* |
1157 |
|
* @param callable the callable action |
1158 |
|
* @return the task |
1159 |
|
*/ |
1160 |
< |
public static <T> ForkJoinTask<T> adapt(Callable<T> callable) { |
1161 |
< |
return new ForkJoinPool.AdaptedCallable<T>(callable); |
1160 |
> |
public static <T> ForkJoinTask<T> adapt(Callable<? extends T> callable) { |
1161 |
> |
return new AdaptedCallable<T>(callable); |
1162 |
|
} |
1163 |
|
|
1164 |
|
// Serialization support |
1169 |
|
* Save the state to a stream. |
1170 |
|
* |
1171 |
|
* @serialData the current run status and the exception thrown |
1172 |
< |
* during execution, or null if none |
1172 |
> |
* during execution, or {@code null} if none |
1173 |
|
* @param s the stream |
1174 |
|
*/ |
1175 |
|
private void writeObject(java.io.ObjectOutputStream s) |
1193 |
|
setDoneExceptionally((Throwable) ex); |
1194 |
|
} |
1195 |
|
|
1196 |
< |
// Unsafe mechanics for jsr166y 3rd party package. |
1196 |
> |
// Unsafe mechanics |
1197 |
> |
|
1198 |
> |
private static final sun.misc.Unsafe UNSAFE = getUnsafe(); |
1199 |
> |
private static final long statusOffset = |
1200 |
> |
objectFieldOffset("status", ForkJoinTask.class); |
1201 |
> |
|
1202 |
> |
private static long objectFieldOffset(String field, Class<?> klazz) { |
1203 |
> |
try { |
1204 |
> |
return UNSAFE.objectFieldOffset(klazz.getDeclaredField(field)); |
1205 |
> |
} catch (NoSuchFieldException e) { |
1206 |
> |
// Convert Exception to corresponding Error |
1207 |
> |
NoSuchFieldError error = new NoSuchFieldError(field); |
1208 |
> |
error.initCause(e); |
1209 |
> |
throw error; |
1210 |
> |
} |
1211 |
> |
} |
1212 |
> |
|
1213 |
> |
/** |
1214 |
> |
* Returns a sun.misc.Unsafe. Suitable for use in a 3rd party package. |
1215 |
> |
* Replace with a simple call to Unsafe.getUnsafe when integrating |
1216 |
> |
* into a jdk. |
1217 |
> |
* |
1218 |
> |
* @return a sun.misc.Unsafe |
1219 |
> |
*/ |
1220 |
|
private static sun.misc.Unsafe getUnsafe() { |
1221 |
|
try { |
1222 |
|
return sun.misc.Unsafe.getUnsafe(); |
1223 |
|
} catch (SecurityException se) { |
1224 |
|
try { |
1225 |
|
return java.security.AccessController.doPrivileged |
1226 |
< |
(new java.security.PrivilegedExceptionAction<sun.misc.Unsafe>() { |
1226 |
> |
(new java.security |
1227 |
> |
.PrivilegedExceptionAction<sun.misc.Unsafe>() { |
1228 |
|
public sun.misc.Unsafe run() throws Exception { |
1229 |
< |
return getUnsafeByReflection(); |
1229 |
> |
java.lang.reflect.Field f = sun.misc |
1230 |
> |
.Unsafe.class.getDeclaredField("theUnsafe"); |
1231 |
> |
f.setAccessible(true); |
1232 |
> |
return (sun.misc.Unsafe) f.get(null); |
1233 |
|
}}); |
1234 |
|
} catch (java.security.PrivilegedActionException e) { |
1235 |
|
throw new RuntimeException("Could not initialize intrinsics", |
1237 |
|
} |
1238 |
|
} |
1239 |
|
} |
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 |
– |
static final long statusOffset = |
1153 |
– |
fieldOffset("status", ForkJoinTask.class); |
1154 |
– |
|
1240 |
|
} |