Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Java

RecursiveTask — a recursive result-bearing task

RecursiveTask<V> represents a fork/join task that returns a result of type V. Its compute() method defines the computation: handle a small problem directly or split it into subtasks and combine their results.

Version: Java SE 21; repository runtime, production runtime not attested

Syntax

class Task extends RecursiveTask<Result> {
    @Override
    protected Result compute() {
        // Replace this placeholder with a base-case return or a return that combines subtask results.
        throw new UnsupportedOperationException("Implement compute()");
    }
}

RecursiveTask<V> is an abstract class in java.util.concurrent that extends ForkJoinTask<V>. It supports divide-and-conquer computations returning a result of type V. For tasks without a result, the counterpart is RecursiveAction.

RecursiveTask.compute has the signature protected abstract V compute(). A subclass implements the base case and the division of work. Calling compute() directly is an ordinary method call: it does not submit the task to a pool. In the example, pool.invoke(...) starts the root task and returns its result.

A typical pattern is left.fork(), a direct right.compute(), and then left.join(). fork() arranges asynchronous execution; join() waits if necessary and returns the result. This does not mean a separate thread for every task. Waiting dependencies must be acyclic to avoid deadlock.

The example sums disjoint ranges [from, to) without modifying the array. The threshold 2 demonstrates splitting, not a performance recommendation: excessively small tasks increase overhead. Avoid blocking I/O in these computations. The code targets Java SE 21; production-runtime compatibility has not been attested.

Examples

import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.RecursiveTask;

public class RecursiveTaskDemo {
    static final class SumTask extends RecursiveTask<Long> {
        private final int[] values;
        private final int from;
        private final int to;

        SumTask(int[] values, int from, int to) {
            this.values = values;
            this.from = from;
            this.to = to;
        }

        @Override
        protected Long compute() {
            if (to - from <= 2) {
                long sum = 0;
                for (int i = from; i < to; i++) {
                    sum += values[i];
                }
                return sum;
            }
            int mid = from + (to - from) / 2;
            SumTask left = new SumTask(values, from, mid);
            SumTask right = new SumTask(values, mid, to);
            left.fork();
            long rightSum = right.compute();
            return rightSum + left.join();
        }
    }

    public static void main(String[] args) {
        int[] values = {1, 2, 3, 4, 5, 6, 7, 8};
        try (ForkJoinPool pool = new ForkJoinPool(2)) {
            System.out.println(pool.invoke(new SumTask(values, 0, values.length)));
        }
    }
}

Result

36

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources