--- jsr166/src/jsr166y/ForkJoinTask.java 2009/07/27 21:41:53 1.24 +++ jsr166/src/jsr166y/ForkJoinTask.java 2009/07/31 16:27:08 1.25 @@ -247,7 +247,7 @@ public abstract class ForkJoinTask im synchronized (this) { try { while (status >= 0) { - long nt = nanos - System.nanoTime() - startTime; + long nt = nanos - (System.nanoTime() - startTime); if (nt <= 0) break; wait(nt / 1000000, (int) (nt % 1000000)); @@ -777,9 +777,10 @@ public abstract class ForkJoinTask im public final V get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException { + long nanos = unit.toNanos(timeout); ForkJoinWorkerThread w = getWorker(); if (w == null || status < 0 || !w.unpushTask(this) || !tryQuietlyInvoke()) - awaitDone(w, unit.toNanos(timeout)); + awaitDone(w, nanos); return reportTimedFutureResult(); } @@ -983,15 +984,18 @@ public abstract class ForkJoinTask im protected abstract boolean exec(); /** - * Returns, but does not unschedule or execute, the task queued by - * the current thread but not yet executed, if one is + * Returns, but does not unschedule or execute, a task queued by + * the current thread but not yet executed, if one is immediately * available. There is no guarantee that this task will actually - * be polled or executed next. This method is designed primarily - * to support extensions, and is unlikely to be useful otherwise. - * This method may be invoked only from within ForkJoinTask - * computations (as may be determined using method {@link - * #inForkJoinPool}). Attempts to invoke in other contexts result - * in exceptions or errors, possibly including ClassCastException. + * be polled or executed next. Conversely, this method may return + * null even if a task exists but cannot be accessed without + * contention with other threads. This method is designed + * primarily to support extensions, and is unlikely to be useful + * otherwise. This method may be invoked only from within + * ForkJoinTask computations (as may be determined using method + * {@link #inForkJoinPool}). Attempts to invoke in other contexts + * result in exceptions or errors, possibly including + * ClassCastException. * * @return the next task, or {@code null} if none are available */ @@ -1038,7 +1042,60 @@ public abstract class ForkJoinTask im .pollTask(); } - // adaptors + /** + * Adaptor for Runnables. This implements RunnableFuture + * to be compliant with AbstractExecutorService constraints + * when used in ForkJoinPool. + */ + static final class AdaptedRunnable extends ForkJoinTask + implements RunnableFuture { + final Runnable runnable; + final T resultOnCompletion; + T result; + AdaptedRunnable(Runnable runnable, T result) { + if (runnable == null) throw new NullPointerException(); + this.runnable = runnable; + this.resultOnCompletion = result; + } + public T getRawResult() { return result; } + public void setRawResult(T v) { result = v; } + public boolean exec() { + runnable.run(); + result = resultOnCompletion; + return true; + } + public void run() { invoke(); } + private static final long serialVersionUID = 5232453952276885070L; + } + + /** + * Adaptor for Callables + */ + static final class AdaptedCallable extends ForkJoinTask + implements RunnableFuture { + final Callable callable; + T result; + AdaptedCallable(Callable callable) { + if (callable == null) throw new NullPointerException(); + this.callable = callable; + } + public T getRawResult() { return result; } + public void setRawResult(T v) { result = v; } + public boolean exec() { + try { + result = callable.call(); + return true; + } catch (Error err) { + throw err; + } catch (RuntimeException rex) { + throw rex; + } catch (Exception ex) { + throw new RuntimeException(ex); + } + } + public void run() { invoke(); } + private static final long serialVersionUID = 2838392045355241008L; + } /** * Returns a new ForkJoinTask that performs the {@code run} @@ -1049,7 +1106,7 @@ public abstract class ForkJoinTask im * @return the task */ public static ForkJoinTask adapt(Runnable runnable) { - return new ForkJoinPool.AdaptedRunnable(runnable, null); + return new AdaptedRunnable(runnable, null); } /** @@ -1062,7 +1119,7 @@ public abstract class ForkJoinTask im * @return the task */ public static ForkJoinTask adapt(Runnable runnable, T result) { - return new ForkJoinPool.AdaptedRunnable(runnable, result); + return new AdaptedRunnable(runnable, result); } /** @@ -1075,7 +1132,7 @@ public abstract class ForkJoinTask im * @return the task */ public static ForkJoinTask adapt(Callable callable) { - return new ForkJoinPool.AdaptedCallable(callable); + return new AdaptedCallable(callable); } // Serialization support