--- jsr166/src/jsr166y/ForkJoinTask.java 2009/07/26 17:33:37 1.22 +++ jsr166/src/jsr166y/ForkJoinTask.java 2009/08/02 22:58:50 1.31 @@ -16,47 +16,48 @@ import java.util.Map; import java.util.WeakHashMap; /** - * Abstract base class for tasks that run within a {@link - * ForkJoinPool}. A ForkJoinTask is a thread-like entity that is much + * Abstract base class for tasks that run within a {@link ForkJoinPool}. + * A {@code ForkJoinTask} is a thread-like entity that is much * lighter weight than a normal thread. Huge numbers of tasks and * subtasks may be hosted by a small number of actual threads in a * ForkJoinPool, at the price of some usage limitations. * - *
A "main" ForkJoinTask begins execution when submitted to a - * {@link ForkJoinPool}. Once started, it will usually in turn start - * other subtasks. As indicated by the name of this class, many - * programs using ForkJoinTasks employ only methods {@code fork} - * and {@code join}, or derivatives such as - * {@code invokeAll}. However, this class also provides a number - * of other methods that can come into play in advanced usages, as - * well as extension mechanics that allow support of new forms of - * fork/join processing. + *
A "main" {@code ForkJoinTask} begins execution when submitted + * to a {@link ForkJoinPool}. Once started, it will usually in turn + * start other subtasks. As indicated by the name of this class, + * many programs using {@code ForkJoinTask} employ only methods + * {@link #fork} and {@link #join}, or derivatives such as {@link + * #invokeAll}. However, this class also provides a number of other + * methods that can come into play in advanced usages, as well as + * extension mechanics that allow support of new forms of fork/join + * processing. * - *
A ForkJoinTask is a lightweight form of {@link Future}. The - * efficiency of ForkJoinTasks stems from a set of restrictions (that - * are only partially statically enforceable) reflecting their - * intended use as computational tasks calculating pure functions or - * operating on purely isolated objects. The primary coordination - * mechanisms are {@link #fork}, that arranges asynchronous execution, - * and {@link #join}, that doesn't proceed until the task's result has - * been computed. Computations should avoid {@code synchronized} - * methods or blocks, and should minimize other blocking - * synchronization apart from joining other tasks or using - * synchronizers such as Phasers that are advertised to cooperate with - * fork/join scheduling. Tasks should also not perform blocking IO, - * and should ideally access variables that are completely independent - * of those accessed by other running tasks. Minor breaches of these - * restrictions, for example using shared output streams, may be - * tolerable in practice, but frequent use may result in poor - * performance, and the potential to indefinitely stall if the number - * of threads not waiting for IO or other external synchronization - * becomes exhausted. This usage restriction is in part enforced by - * not permitting checked exceptions such as {@code IOExceptions} - * to be thrown. However, computations may still encounter unchecked - * exceptions, that are rethrown to callers attempting join - * them. These exceptions may additionally include - * RejectedExecutionExceptions stemming from internal resource - * exhaustion such as failure to allocate internal task queues. + *
A {@code ForkJoinTask} is a lightweight form of {@link Future}. + * The efficiency of {@code ForkJoinTask}s stems from a set of + * restrictions (that are only partially statically enforceable) + * reflecting their intended use as computational tasks calculating + * pure functions or operating on purely isolated objects. The + * primary coordination mechanisms are {@link #fork}, that arranges + * asynchronous execution, and {@link #join}, that doesn't proceed + * until the task's result has been computed. Computations should + * avoid {@code synchronized} methods or blocks, and should minimize + * other blocking synchronization apart from joining other tasks or + * using synchronizers such as Phasers that are advertised to + * cooperate with fork/join scheduling. Tasks should also not perform + * blocking IO, and should ideally access variables that are + * completely independent of those accessed by other running + * tasks. Minor breaches of these restrictions, for example using + * shared output streams, may be tolerable in practice, but frequent + * use may result in poor performance, and the potential to + * indefinitely stall if the number of threads not waiting for IO or + * other external synchronization becomes exhausted. This usage + * restriction is in part enforced by not permitting checked + * exceptions such as {@code IOExceptions} to be thrown. However, + * computations may still encounter unchecked exceptions, that are + * rethrown to callers attempting join them. These exceptions may + * additionally include RejectedExecutionExceptions stemming from + * internal resource exhaustion such as failure to allocate internal + * task queues. * *
The primary method for awaiting completion and extracting * results of a task is {@link #join}, but there are several variants: @@ -75,9 +76,11 @@ import java.util.WeakHashMap; * performs the most common form of parallel invocation: forking a set * of tasks and joining them all. * - *
The ForkJoinTask class is not usually directly subclassed. + *
The ForkJoinTask class is not usually directly subclassed. * Instead, you subclass one of the abstract classes that support a - * particular style of fork/join processing. Normally, a concrete + * particular style of fork/join processing, typically {@link + * RecursiveAction} for computations that do not return results, or + * {@link RecursiveTask} for those that do. Normally, a concrete * ForkJoinTask subclass declares fields comprising its parameters, * established in a constructor, and then defines a {@code compute} * method that somehow uses the control methods supplied by this base @@ -94,8 +97,8 @@ import java.util.WeakHashMap; * lightweight task scheduling framework, and so cannot be overridden. * Developers creating new basic styles of fork/join processing should * minimally implement {@code protected} methods - * {@code exec}, {@code setRawResult}, and - * {@code getRawResult}, while also introducing an abstract + * {@link #exec}, {@link #setRawResult}, and + * {@link #getRawResult}, while also introducing an abstract * computational method that can be implemented in its subclasses, * possibly relying on other {@code protected} methods provided * by this class. @@ -107,6 +110,12 @@ import java.util.WeakHashMap; * parallelism cannot improve throughput. If too small, then memory * and internal task maintenance overhead may overwhelm processing. * + *
This class provides {@code adapt} methods for {@link + * java.lang.Runnable} and {@link java.util.concurrent.Callable}, that + * may be of use when mixing execution of ForkJoinTasks with other + * kinds of tasks. When all tasks are of this form, consider using a + * pool in {@link ForkJoinPool#setAsyncMode}. + * *
ForkJoinTasks are {@code Serializable}, which enables them
* to be used in extensions such as remote execution frameworks. It is
* in general sensible to serialize tasks only before or after, but
@@ -248,7 +257,7 @@ public abstract class ForkJoinTask This method may be invoked only from within {@code
+ * ForkJoinTask} computations (as may be determined using method
+ * {@link #inForkJoinPool}). Attempts to invoke in other contexts
+ * result in exceptions or errors, possibly including {@code
+ * ClassCastException}.
*
- * @return {@code this}, to simplify usage.
+ * @return {@code this}, to simplify usage
*/
public final ForkJoinTask This method may be invoked only from within {@code
+ * ForkJoinTask} computations (as may be determined using method
+ * {@link #inForkJoinPool}). Attempts to invoke in other contexts
+ * result in exceptions or errors, possibly including {@code
+ * ClassCastException}.
+ *
+ * @param t1 the first task
+ * @param t2 the second task
+ * @throws NullPointerException if any task is null
+ * @throws RuntimeException or Error if a task did so
*/
- public static void invokeAll(ForkJoinTask>t1, ForkJoinTask> t2) {
+ public static void invokeAll(ForkJoinTask> t1, ForkJoinTask> t2) {
t2.fork();
t1.invoke();
t2.join();
}
/**
- * Forks the given tasks, returning when {@code isDone} holds
- * for all of them. If any task encounters an exception, others
- * may be cancelled. 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.
+ * Forks the given tasks, returning when {@code isDone} holds for
+ * each task or an exception is encountered. If any task
+ * encounters an exception, others may be, but are not guaranteed
+ * to be, cancelled.
+ *
+ * This method may be invoked only from within {@code
+ * ForkJoinTask} computations (as may be determined using method
+ * {@link #inForkJoinPool}). Attempts to invoke in other contexts
+ * result in exceptions or errors, possibly including {@code
+ * ClassCastException}.
+ *
+ * Overloadings of this method exist for the special cases
+ * of one to four arguments.
*
- * @param tasks the array of tasks
+ * @param tasks the tasks
* @throws NullPointerException if tasks or any element are null
* @throws RuntimeException or Error if any task did so
*/
@@ -597,13 +616,16 @@ public abstract class ForkJoinTask This method may be invoked only from within {@code
+ * ForkJoinTask} computations (as may be determined using method
+ * {@link #inForkJoinPool}). Attempts to invoke in other contexts
+ * result in exceptions or errors, possibly including {@code
+ * ClassCastException}.
*
* @param tasks the collection of tasks
* @return the tasks argument, to simplify usage
@@ -652,19 +674,19 @@ public abstract class ForkJoinTask This method may be overridden in subclasses, but if so, must
* still ensure that these minimal properties hold. In particular,
- * the cancel method itself must not throw exceptions.
+ * the {@code cancel} method itself must not throw exceptions.
*
- * This method is designed to be invoked by other
+ * This method is designed to be invoked by other
* tasks. To terminate the current task, you can just return or
* throw an unchecked exception from its computation method, or
- * invoke {@code completeExceptionally}.
+ * invoke {@link #completeExceptionally}.
*
* @param mayInterruptIfRunning this value is ignored in the
* default implementation because tasks are not in general
* cancelled via interruption
*
- * @return true if this task is now cancelled
+ * @return {@code true} if this task is now cancelled
*/
public boolean cancel(boolean mayInterruptIfRunning) {
setCompletion(CANCELLED);
@@ -703,9 +725,9 @@ public abstract class ForkJoinTask This method may be invoked only from within {@code
+ * ForkJoinTask} computations (as may be determined using method
+ * {@link #inForkJoinPool}). Attempts to invoke in other contexts
+ * result in exceptions or errors, possibly including {@code
+ * ClassCastException}.
*
* @return the computed result
*/
@@ -807,11 +832,13 @@ public abstract class ForkJoinTask This method may be invoked only from within {@code
+ * ForkJoinTask} computations (as may be determined using method
+ * {@link #inForkJoinPool}). Attempts to invoke in other contexts
+ * result in exceptions or errors, possibly including {@code
+ * ClassCastException}.
*/
public final void quietlyHelpJoin() {
if (status >= 0) {
@@ -853,6 +880,12 @@ public abstract class ForkJoinTask This method may be invoked only from within {@code
+ * ForkJoinTask} computations (as may be determined using method
+ * {@link #inForkJoinPool}). Attempts to invoke in other contexts
+ * result in exceptions or errors, possibly including {@code
+ * ClassCastException}.
*/
public static void helpQuiesce() {
((ForkJoinWorkerThread) Thread.currentThread())
@@ -865,8 +898,8 @@ public abstract class ForkJoinTask This method may be invoked only from within {@code
+ * ForkJoinTask} computations (as may be determined using method
+ * {@link #inForkJoinPool}). Attempts to invoke in other contexts
+ * result in exceptions or errors, possibly including {@code
+ * ClassCastException}.
*
- * @return true if unforked
+ * @return {@code true} if unforked
*/
public boolean tryUnfork() {
return ((ForkJoinWorkerThread) Thread.currentThread())
@@ -923,6 +959,12 @@ public abstract class ForkJoinTask This method may be invoked only from within {@code
+ * ForkJoinTask} computations (as may be determined using method
+ * {@link #inForkJoinPool}). Attempts to invoke in other contexts
+ * result in exceptions or errors, possibly including {@code
+ * ClassCastException}.
+ *
* @return the number of tasks
*/
public static int getQueuedTaskCount() {
@@ -940,6 +982,12 @@ public abstract class ForkJoinTask This method may be invoked only from within {@code
+ * ForkJoinTask} computations (as may be determined using method
+ * {@link #inForkJoinPool}). Attempts to invoke in other contexts
+ * result in exceptions or errors, possibly including {@code
+ * ClassCastException}.
+ *
* @return the surplus number of tasks, which may be negative
*/
public static int getSurplusQueuedTaskCount() {
@@ -950,13 +998,13 @@ public abstract class ForkJoinTask This method may be invoked only from within {@code
+ * ForkJoinTask} computations (as may be determined using method
+ * {@link #inForkJoinPool}). Attempts to invoke in other contexts
+ * result in exceptions or errors, possibly including {@code
+ * ClassCastException}.
*
- * @return the next task, or null if none are available
+ * @return the next task, or {@code null} if none are available
*/
protected static ForkJoinTask> peekNextLocalTask() {
return ((ForkJoinWorkerThread) Thread.currentThread())
@@ -1005,13 +1058,15 @@ public abstract class ForkJoinTask This method may be invoked only from within {@code
+ * ForkJoinTask} computations (as may be determined using method
+ * {@link #inForkJoinPool}). Attempts to invoke in other contexts
+ * result in exceptions or errors, possibly including {@code
+ * ClassCastException}.
+ *
+ * @return the next task, or {@code null} if none are available
*/
protected static ForkJoinTask> pollNextLocalTask() {
return ((ForkJoinWorkerThread) Thread.currentThread())
@@ -1026,57 +1081,112 @@ public abstract class ForkJoinTask This method may be invoked only from within {@code
+ * ForkJoinTask} computations (as may be determined using method
+ * {@link #inForkJoinPool}). Attempts to invoke in other contexts
+ * result in exceptions or errors, possibly including {@code
+ * ClassCastException}.
*
- * @return a task, or null if none are available
+ * @return a task, or {@code null} if none are available
*/
protected static ForkJoinTask> pollTask() {
return ((ForkJoinWorkerThread) Thread.currentThread())
.pollTask();
}
- // adaptors
+ /**
+ * Adaptor for Runnables. This implements RunnableFuture
+ * to be compliant with AbstractExecutorService constraints
+ * when used in ForkJoinPool.
+ */
+ static final class AdaptedRunnable