--- jsr166/src/jsr166y/ForkJoinTask.java 2009/07/25 00:34:00 1.17 +++ jsr166/src/jsr166y/ForkJoinTask.java 2009/07/25 15:50:57 1.18 @@ -492,10 +492,13 @@ public abstract class ForkJoinTask im * computations (as may be determined using method {@link * #inForkJoinPool}). Attempts to invoke in other contexts result * in exceptions or errors, possibly including ClassCastException. + * + * @return this, to simplify usage. */ - public final void fork() { + public final ForkJoinTask fork() { ((ForkJoinWorkerThread) Thread.currentThread()) .pushTask(this); + return this; } /** @@ -1034,6 +1037,46 @@ public abstract class ForkJoinTask im .pollTask(); } + // adaptors + + /** + * Returns a new ForkJoinTask that performs the run + * method of the given Runnable as its action, and returns a null + * result upon join. + * + * @param runnable the runnable action + * @return the task + */ + public static ForkJoinTask adapt(Runnable runnable) { + return new ForkJoinPool.AdaptedRunnable(runnable, null); + } + + /** + * Returns a new ForkJoinTask that performs the run + * method of the given Runnable as its action, and returns the + * given result upon join. + * + * @param runnable the runnable action + * @param result the result upon completion + * @return the task + */ + public static ForkJoinTask adapt(Runnable runnable, T result) { + return new ForkJoinPool.AdaptedRunnable(runnable, result); + } + + /** + * Returns a new ForkJoinTask that performs the call + * method of the given Callable as its action, and returns its + * result upon join, translating any checked + * exceptions encountered into RuntimeException. + * + * @param callable the callable action + * @return the task + */ + public static ForkJoinTask adapt(Callable callable) { + return new ForkJoinPool.AdaptedCallable(callable); + } + // Serialization support private static final long serialVersionUID = -7721805057305804111L;