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. |
28 |
> |
* programs using ForkJoinTasks employ only methods {@code fork} and |
29 |
> |
* {@code join}, or derivatives such as {@code invokeAll}. However, |
30 |
> |
* this class also provides a number of other methods that can come |
31 |
> |
* into play in advanced usages, as well as extension mechanics that |
32 |
> |
* allow support of new forms of fork/join processing. |
33 |
|
* |
34 |
|
* <p>A ForkJoinTask is a lightweight form of {@link Future}. The |
35 |
|
* efficiency of ForkJoinTasks stems from a set of restrictions (that |
93 |
|
* lightweight task scheduling framework, and so cannot be overridden. |
94 |
|
* Developers creating new basic styles of fork/join processing should |
95 |
|
* minimally implement {@code protected} methods |
96 |
< |
* {@code exec}, {@code setRawResult}, and |
97 |
< |
* {@code getRawResult}, while also introducing an abstract |
96 |
> |
* {@link #exec}, {@link #setRawResult}, and |
97 |
> |
* {@link #getRawResult}, while also introducing an abstract |
98 |
|
* computational method that can be implemented in its subclasses, |
99 |
|
* possibly relying on other {@code protected} methods provided |
100 |
|
* by this class. |
247 |
|
synchronized (this) { |
248 |
|
try { |
249 |
|
while (status >= 0) { |
250 |
< |
long nt = nanos - System.nanoTime() - startTime; |
250 |
> |
long nt = nanos - (System.nanoTime() - startTime); |
251 |
|
if (nt <= 0) |
252 |
|
break; |
253 |
|
wait(nt / 1000000, (int) (nt % 1000000)); |
672 |
|
/** |
673 |
|
* Asserts that the results of this task's computation will not be |
674 |
|
* used. If a cancellation occurs before attempting to execute this |
675 |
< |
* task, then execution will be suppressed, {@code isCancelled} |
676 |
< |
* will report true, and {@code join} will result in a |
675 |
> |
* task, execution will be suppressed, {@link #isCancelled} |
676 |
> |
* will report true, and {@link #join} will result in a |
677 |
|
* {@code CancellationException} being thrown. Otherwise, when |
678 |
|
* cancellation races with completion, there are no guarantees |
679 |
< |
* about whether {@code isCancelled} will report true, whether |
680 |
< |
* {@code join} will return normally or via an exception, or |
681 |
< |
* whether these behaviors will remain consistent upon repeated |
679 |
> |
* about whether {@code isCancelled} will report {@code true}, |
680 |
> |
* whether {@code join} will return normally or via an exception, |
681 |
> |
* or whether these behaviors will remain consistent upon repeated |
682 |
|
* invocation. |
683 |
|
* |
684 |
|
* <p>This method may be overridden in subclasses, but if so, must |
688 |
|
* <p> This method is designed to be invoked by <em>other</em> |
689 |
|
* tasks. To terminate the current task, you can just return or |
690 |
|
* throw an unchecked exception from its computation method, or |
691 |
< |
* invoke {@code completeExceptionally}. |
691 |
> |
* invoke {@link #completeExceptionally}. |
692 |
|
* |
693 |
|
* @param mayInterruptIfRunning this value is ignored in the |
694 |
|
* default implementation because tasks are not in general |
777 |
|
|
778 |
|
public final V get(long timeout, TimeUnit unit) |
779 |
|
throws InterruptedException, ExecutionException, TimeoutException { |
780 |
+ |
long nanos = unit.toNanos(timeout); |
781 |
|
ForkJoinWorkerThread w = getWorker(); |
782 |
|
if (w == null || status < 0 || !w.unpushTask(this) || !tryQuietlyInvoke()) |
783 |
< |
awaitDone(w, unit.toNanos(timeout)); |
783 |
> |
awaitDone(w, nanos); |
784 |
|
return reportTimedFutureResult(); |
785 |
|
} |
786 |
|
|
984 |
|
protected abstract boolean exec(); |
985 |
|
|
986 |
|
/** |
987 |
< |
* Returns, but does not unschedule or execute, the task queued by |
988 |
< |
* the current thread but not yet executed, if one is |
987 |
> |
* Returns, but does not unschedule or execute, a task queued by |
988 |
> |
* the current thread but not yet executed, if one is immediately |
989 |
|
* available. There is no guarantee that this task will actually |
990 |
< |
* be polled or executed next. This method is designed primarily |
991 |
< |
* to support extensions, and is unlikely to be useful otherwise. |
992 |
< |
* This method may be invoked only from within ForkJoinTask |
993 |
< |
* computations (as may be determined using method {@link |
994 |
< |
* #inForkJoinPool}). Attempts to invoke in other contexts result |
995 |
< |
* in exceptions or errors, possibly including ClassCastException. |
990 |
> |
* be polled or executed next. Conversely, this method may return |
991 |
> |
* null even if a task exists but cannot be accessed without |
992 |
> |
* contention with other threads. This method is designed |
993 |
> |
* primarily to support extensions, and is unlikely to be useful |
994 |
> |
* otherwise. This method may be invoked only from within |
995 |
> |
* ForkJoinTask computations (as may be determined using method |
996 |
> |
* {@link #inForkJoinPool}). Attempts to invoke in other contexts |
997 |
> |
* result in exceptions or errors, possibly including |
998 |
> |
* ClassCastException. |
999 |
|
* |
1000 |
|
* @return the next task, or {@code null} if none are available |
1001 |
|
*/ |
1042 |
|
.pollTask(); |
1043 |
|
} |
1044 |
|
|
1045 |
< |
// adaptors |
1045 |
> |
/** |
1046 |
> |
* Adaptor for Runnables. This implements RunnableFuture |
1047 |
> |
* to be compliant with AbstractExecutorService constraints |
1048 |
> |
* when used in ForkJoinPool. |
1049 |
> |
*/ |
1050 |
> |
static final class AdaptedRunnable<T> extends ForkJoinTask<T> |
1051 |
> |
implements RunnableFuture<T> { |
1052 |
> |
final Runnable runnable; |
1053 |
> |
final T resultOnCompletion; |
1054 |
> |
T result; |
1055 |
> |
AdaptedRunnable(Runnable runnable, T result) { |
1056 |
> |
if (runnable == null) throw new NullPointerException(); |
1057 |
> |
this.runnable = runnable; |
1058 |
> |
this.resultOnCompletion = result; |
1059 |
> |
} |
1060 |
> |
public T getRawResult() { return result; } |
1061 |
> |
public void setRawResult(T v) { result = v; } |
1062 |
> |
public boolean exec() { |
1063 |
> |
runnable.run(); |
1064 |
> |
result = resultOnCompletion; |
1065 |
> |
return true; |
1066 |
> |
} |
1067 |
> |
public void run() { invoke(); } |
1068 |
> |
private static final long serialVersionUID = 5232453952276885070L; |
1069 |
> |
} |
1070 |
> |
|
1071 |
> |
/** |
1072 |
> |
* Adaptor for Callables |
1073 |
> |
*/ |
1074 |
> |
static final class AdaptedCallable<T> extends ForkJoinTask<T> |
1075 |
> |
implements RunnableFuture<T> { |
1076 |
> |
final Callable<T> callable; |
1077 |
> |
T result; |
1078 |
> |
AdaptedCallable(Callable<T> callable) { |
1079 |
> |
if (callable == null) throw new NullPointerException(); |
1080 |
> |
this.callable = callable; |
1081 |
> |
} |
1082 |
> |
public T getRawResult() { return result; } |
1083 |
> |
public void setRawResult(T v) { result = v; } |
1084 |
> |
public boolean exec() { |
1085 |
> |
try { |
1086 |
> |
result = callable.call(); |
1087 |
> |
return true; |
1088 |
> |
} catch (Error err) { |
1089 |
> |
throw err; |
1090 |
> |
} catch (RuntimeException rex) { |
1091 |
> |
throw rex; |
1092 |
> |
} catch (Exception ex) { |
1093 |
> |
throw new RuntimeException(ex); |
1094 |
> |
} |
1095 |
> |
} |
1096 |
> |
public void run() { invoke(); } |
1097 |
> |
private static final long serialVersionUID = 2838392045355241008L; |
1098 |
> |
} |
1099 |
|
|
1100 |
|
/** |
1101 |
|
* Returns a new ForkJoinTask that performs the {@code run} |
1106 |
|
* @return the task |
1107 |
|
*/ |
1108 |
|
public static ForkJoinTask<Void> adapt(Runnable runnable) { |
1109 |
< |
return new ForkJoinPool.AdaptedRunnable<Void>(runnable, null); |
1109 |
> |
return new AdaptedRunnable<Void>(runnable, null); |
1110 |
|
} |
1111 |
|
|
1112 |
|
/** |
1119 |
|
* @return the task |
1120 |
|
*/ |
1121 |
|
public static <T> ForkJoinTask<T> adapt(Runnable runnable, T result) { |
1122 |
< |
return new ForkJoinPool.AdaptedRunnable<T>(runnable, result); |
1122 |
> |
return new AdaptedRunnable<T>(runnable, result); |
1123 |
|
} |
1124 |
|
|
1125 |
|
/** |
1132 |
|
* @return the task |
1133 |
|
*/ |
1134 |
|
public static <T> ForkJoinTask<T> adapt(Callable<T> callable) { |
1135 |
< |
return new ForkJoinPool.AdaptedCallable<T>(callable); |
1135 |
> |
return new AdaptedCallable<T>(callable); |
1136 |
|
} |
1137 |
|
|
1138 |
|
// Serialization support |