245 |
|
synchronized (this) { |
246 |
|
try { |
247 |
|
while (status >= 0) { |
248 |
< |
long nt = nanos - System.nanoTime() - startTime; |
248 |
> |
long nt = nanos - (System.nanoTime() - startTime); |
249 |
|
if (nt <= 0) |
250 |
|
break; |
251 |
|
wait(nt / 1000000, (int) (nt % 1000000)); |
775 |
|
|
776 |
|
public final V get(long timeout, TimeUnit unit) |
777 |
|
throws InterruptedException, ExecutionException, TimeoutException { |
778 |
+ |
long nanos = unit.toNanos(timeout); |
779 |
|
ForkJoinWorkerThread w = getWorker(); |
780 |
|
if (w == null || status < 0 || !w.unpushTask(this) || !tryQuietlyInvoke()) |
781 |
< |
awaitDone(w, unit.toNanos(timeout)); |
781 |
> |
awaitDone(w, nanos); |
782 |
|
return reportTimedFutureResult(); |
783 |
|
} |
784 |
|
|
982 |
|
protected abstract boolean exec(); |
983 |
|
|
984 |
|
/** |
985 |
< |
* Returns, but does not unschedule or execute, the task queued by |
986 |
< |
* the current thread but not yet executed, if one is |
985 |
> |
* Returns, but does not unschedule or execute, a task queued by |
986 |
> |
* the current thread but not yet executed, if one is immediately |
987 |
|
* available. There is no guarantee that this task will actually |
988 |
< |
* be polled or executed next. This method is designed primarily |
989 |
< |
* to support extensions, and is unlikely to be useful otherwise. |
990 |
< |
* This method may be invoked only from within ForkJoinTask |
991 |
< |
* computations (as may be determined using method {@link |
992 |
< |
* #inForkJoinPool}). Attempts to invoke in other contexts result |
993 |
< |
* in exceptions or errors, possibly including ClassCastException. |
988 |
> |
* be polled or executed next. Conversely, this method may return |
989 |
> |
* null even if a task exists but cannot be accessed without |
990 |
> |
* contention with other threads. This method is designed |
991 |
> |
* primarily to support extensions, and is unlikely to be useful |
992 |
> |
* otherwise. This method may be invoked only from within |
993 |
> |
* ForkJoinTask computations (as may be determined using method |
994 |
> |
* {@link #inForkJoinPool}). Attempts to invoke in other contexts |
995 |
> |
* result in exceptions or errors, possibly including |
996 |
> |
* ClassCastException. |
997 |
|
* |
998 |
|
* @return the next task, or {@code null} if none are available |
999 |
|
*/ |
1040 |
|
.pollTask(); |
1041 |
|
} |
1042 |
|
|
1043 |
< |
// adaptors |
1043 |
> |
/** |
1044 |
> |
* Adaptor for Runnables. This implements RunnableFuture |
1045 |
> |
* to be compliant with AbstractExecutorService constraints |
1046 |
> |
* when used in ForkJoinPool. |
1047 |
> |
*/ |
1048 |
> |
static final class AdaptedRunnable<T> extends ForkJoinTask<T> |
1049 |
> |
implements RunnableFuture<T> { |
1050 |
> |
final Runnable runnable; |
1051 |
> |
final T resultOnCompletion; |
1052 |
> |
T result; |
1053 |
> |
AdaptedRunnable(Runnable runnable, T result) { |
1054 |
> |
if (runnable == null) throw new NullPointerException(); |
1055 |
> |
this.runnable = runnable; |
1056 |
> |
this.resultOnCompletion = result; |
1057 |
> |
} |
1058 |
> |
public T getRawResult() { return result; } |
1059 |
> |
public void setRawResult(T v) { result = v; } |
1060 |
> |
public boolean exec() { |
1061 |
> |
runnable.run(); |
1062 |
> |
result = resultOnCompletion; |
1063 |
> |
return true; |
1064 |
> |
} |
1065 |
> |
public void run() { invoke(); } |
1066 |
> |
private static final long serialVersionUID = 5232453952276885070L; |
1067 |
> |
} |
1068 |
> |
|
1069 |
> |
/** |
1070 |
> |
* Adaptor for Callables |
1071 |
> |
*/ |
1072 |
> |
static final class AdaptedCallable<T> extends ForkJoinTask<T> |
1073 |
> |
implements RunnableFuture<T> { |
1074 |
> |
final Callable<T> callable; |
1075 |
> |
T result; |
1076 |
> |
AdaptedCallable(Callable<T> callable) { |
1077 |
> |
if (callable == null) throw new NullPointerException(); |
1078 |
> |
this.callable = callable; |
1079 |
> |
} |
1080 |
> |
public T getRawResult() { return result; } |
1081 |
> |
public void setRawResult(T v) { result = v; } |
1082 |
> |
public boolean exec() { |
1083 |
> |
try { |
1084 |
> |
result = callable.call(); |
1085 |
> |
return true; |
1086 |
> |
} catch (Error err) { |
1087 |
> |
throw err; |
1088 |
> |
} catch (RuntimeException rex) { |
1089 |
> |
throw rex; |
1090 |
> |
} catch (Exception ex) { |
1091 |
> |
throw new RuntimeException(ex); |
1092 |
> |
} |
1093 |
> |
} |
1094 |
> |
public void run() { invoke(); } |
1095 |
> |
private static final long serialVersionUID = 2838392045355241008L; |
1096 |
> |
} |
1097 |
|
|
1098 |
|
/** |
1099 |
|
* Returns a new ForkJoinTask that performs the {@code run} |
1104 |
|
* @return the task |
1105 |
|
*/ |
1106 |
|
public static ForkJoinTask<Void> adapt(Runnable runnable) { |
1107 |
< |
return new ForkJoinPool.AdaptedRunnable<Void>(runnable, null); |
1107 |
> |
return new AdaptedRunnable<Void>(runnable, null); |
1108 |
|
} |
1109 |
|
|
1110 |
|
/** |
1117 |
|
* @return the task |
1118 |
|
*/ |
1119 |
|
public static <T> ForkJoinTask<T> adapt(Runnable runnable, T result) { |
1120 |
< |
return new ForkJoinPool.AdaptedRunnable<T>(runnable, result); |
1120 |
> |
return new AdaptedRunnable<T>(runnable, result); |
1121 |
|
} |
1122 |
|
|
1123 |
|
/** |
1130 |
|
* @return the task |
1131 |
|
*/ |
1132 |
|
public static <T> ForkJoinTask<T> adapt(Callable<T> callable) { |
1133 |
< |
return new ForkJoinPool.AdaptedCallable<T>(callable); |
1133 |
> |
return new AdaptedCallable<T>(callable); |
1134 |
|
} |
1135 |
|
|
1136 |
|
// Serialization support |