Added in API level 21

RecursiveTask

abstract class RecursiveTask<V : Any!> : ForkJoinTask<V>
kotlin.Any
   ↳ java.util.concurrent.ForkJoinTask<V>
   ↳ java.util.concurrent.RecursiveTask

A recursive result-bearing ForkJoinTask.

For a classic example, here is a task computing Fibonacci numbers:

<code>class Fibonacci extends RecursiveTask&lt;Integer&gt; {
    final int n;
    Fibonacci(int n) { this.n = n; }
    protected Integer compute() {
      if (n &lt;= 1)
        return n;
      Fibonacci f1 = new Fibonacci(n - 1);
      f1.fork();
      Fibonacci f2 = new Fibonacci(n - 2);
      return f2.compute() + f1.join();
    }
  }</code>
However, besides being a dumb way to compute Fibonacci functions (there is a simple fast linear algorithm that you'd use in practice), this is likely to perform poorly because the smallest subtasks are too small to be worthwhile splitting up. Instead, as is the case for nearly all fork/join applications, you'd pick some minimum granularity size (for example 10 here) for which you always sequentially solve rather than subdividing.

Summary

Public constructors

Constructor for subclasses to call.

Public methods
V

Protected methods
abstract V

The main computation performed by this task.

Boolean

Implements execution conventions for RecursiveTask.

Unit
setRawResult(value: V)

Inherited functions

Public constructors

RecursiveTask

Added in API level 21
RecursiveTask()

Constructor for subclasses to call.

Public methods

getRawResult

Added in API level 21
fun getRawResult(): V
Return
V the result, or null if not completed

Protected methods

compute

Added in API level 21
protected abstract fun compute(): V

The main computation performed by this task.

Return
V the result of the computation

exec

Added in API level 21
protected fun exec(): Boolean

Implements execution conventions for RecursiveTask.

Return
Boolean true if this task is known to have completed normally

setRawResult

Added in API level 21
protected fun setRawResult(value: V): Unit
Parameters
value V: the value