APIdia Launch Partner
Top Description Fields Constructors Methods
java.util.concurrent

public abstract Class RecursiveTask<V>

extends ForkJoinTask<V>
Class Inheritance
Known Direct Subclasses
java.util.DualPivotQuicksort.RunMerger, java.math.BigInteger.RecursiveOp
Type Parameters
<V>
the type of the result of the task

A recursive result-bearing ForkJoinTask.

For example, here is a task-based program for computing Factorials:

 import java.util.concurrent.RecursiveTask;
import java.math.BigInteger;
public class Factorial {
  static class FactorialTask extends RecursiveTask<BigInteger> {
    private final int from, to;
    FactorialTask(int from, int to) { this.from = from; this.to = to; }
    protected BigInteger compute() {
      int range = to - from;
      if (range == 0) {                       // base case
        return BigInteger.valueOf(from);
      } else if (range == 1) {                // too small to parallelize
        return BigInteger.valueOf(from).multiply(BigInteger.valueOf(to));
      } else {                                // split in half
        int mid = from + range / 2;
        FactorialTask leftTask = new FactorialTask(from, mid);
        leftTask.fork();         // perform about half the work locally
        return new FactorialTask(mid + 1, to).compute()
               .multiply(leftTask.join());
      }
    }
  }
  static BigInteger factorial(int n) { // uses ForkJoinPool.commonPool()
    return (n <= 1) ? BigInteger.ONE : new FactorialTask(1, n).invoke();
  }
  public static void main(String[] args) {
    System.out.println(factorial(Integer.parseInt(args[0])));
  }
}
Author
Doug Lea
Since
1.7

Field Summary

Modifier and TypeField and Description
pack-priv V
private static final long
Inherited from java.util.concurrent.ForkJoinTask:
ABNORMALDONEHAVE_EXCEPTIONMARKERNO_USER_HELPNUH_BITSMASKstatusTHROWNUNCOMPENSATE

Constructor Summary

AccessConstructor and Description
public
RecursiveTask()

Constructor for subclasses to call.

Method Summary

Modifier and TypeMethod and Description
protected abstract V

Returns:

the result of the computation
compute
()

The main computation performed by this task.

protected final boolean
exec()

Implements abstract java.util.concurrent.ForkJoinTask.exec.

Implements execution conventions for RecursiveTask.

public final V
getRawResult()

Implements abstract java.util.concurrent.ForkJoinTask.getRawResult.

Returns the result that would be returned by join, even if this task completed abnormally, or null if this task is not known to have been completed.

protected final void
setRawResult(V value)

Implements abstract java.util.concurrent.ForkJoinTask.setRawResult.

Forces the given value to be returned as a result.

Inherited from java.util.concurrent.ForkJoinTask:
adaptadaptadaptadaptInterruptibleadaptInterruptibleadaptInterruptiblecancelcompareAndSetForkJoinTaskTagcompletecompleteExceptionallydoExecexceptionNowforkgetgetgetExceptiongetForkJoinTaskStatusMarkerBitgetForkJoinTaskTaggetPoolgetQueuedTaskCountgetSurplusQueuedTaskCounthelpQuiesceinForkJoinPoolinvokeinvokeAllinvokeAllinvokeAllisCancelledisCompletedAbnormallyisCompletedNormallyisDonejoinnoUserHelponAuxExceptionSetpeekNextLocalTaskpollNextLocalTaskpollSubmissionpollTaskquietlyCompletequietlyInvokequietlyJoinquietlyJoinquietlyJoinPoolInvokeAllTaskquietlyJoinUninterruptiblyreinitializeresultNowrethrowsetForkJoinTaskStatusMarkerBitsetForkJoinTaskTagsetNoUserHelpstatetrySetCancelledtrySetExceptiontrySetThrowntryUnforkuncheckedThrow

Field Detail

resultback to summary
pack-priv V result
Annotations
@SuppressWarnings:serial
serialVersionUIDback to summary
private static final long serialVersionUID

Hides java.util.concurrent.ForkJoinTask.serialVersionUID.

Constructor Detail

RecursiveTaskback to summary
public RecursiveTask()

Constructor for subclasses to call.

Method Detail

computeback to summary
protected abstract V compute()

The main computation performed by this task.

Returns:V

the result of the computation

execback to summary
protected final boolean exec()

Implements abstract java.util.concurrent.ForkJoinTask.exec.

Implements execution conventions for RecursiveTask.

getRawResultback to summary
public final V getRawResult()

Implements abstract java.util.concurrent.ForkJoinTask.getRawResult.

Doc from java.util.concurrent.ForkJoinTask.getRawResult.

Returns the result that would be returned by join, even if this task completed abnormally, or null if this task is not known to have been completed. This method is designed to aid debugging, as well as to support extensions. Its use in any other context is discouraged.

setRawResultback to summary
protected final void setRawResult(V value)

Implements abstract java.util.concurrent.ForkJoinTask.setRawResult.

Doc from java.util.concurrent.ForkJoinTask.setRawResult.

Forces the given value to be returned as a result. This method is designed to support extensions, and should not in general be called otherwise.