concept RecursiveTask in category java

appears as: RecursiveTask, RecursiveTask
Modern Java in Action: Lambdas, streams, reactive and functional programming

This is an excerpt from Manning's book Modern Java in Action: Lambdas, streams, reactive and functional programming.

7.2.1. Working with RecursiveTask

To submit tasks to this pool, you have to create a subclass of RecursiveTask<R>, where R is the type of the result produced by the parallelized task (and each of its subtasks) or of RecursiveAction if the task returns no result (it could be updating other nonlocal structures, though). To define RecursiveTasks you need only implement its single abstract method, compute:

Java 8 in Action: Lambdas, streams, and functional-style programming

This is an excerpt from Manning's book Java 8 in Action: Lambdas, streams, and functional-style programming.

7.2.1. Working with RecursiveTask

To submit tasks to this pool, you have to create a subclass of RecursiveTask<R>, where R is the type of the result produced by the parallelized task (and each of its subtasks) or of RecursiveAction if the task returns no result (it could be updating other nonlocal structures, though). To define RecursiveTasks you need only implement its single abstract method, compute:

As you might have noticed, this is nothing more than the parallel version of the well-known divide-and-conquer algorithm. To demonstrate a practical example of how to use the fork/join framework and to build on our previous examples, let’s try to calculate the sum of a range of numbers (here represented by an array of numbers long[]) using this framework. As explained, you need to first provide an implementation for the RecursiveTask class, as shown by the ForkJoinSumCalculator in the following listing.

Listing 7.2. Executing a parallel sum using the fork/join framework
public class ForkJoinSumCalculator extends java.util.concurrent.RecursiveTask<Long> { #1 private final long[] numbers; #2 private final int start; #3 private final int end; public static final long THRESHOLD = 10_000; #4 public ForkJoinSumCalculator(long[] numbers) { #5 this(numbers, 0, numbers.length); } private ForkJoinSumCalculator(long[] numbers, int start, int end) { #6 this.numbers = numbers; this.start = start; this.end = end; } @Override protected Long compute() { #7 int length = end - start; #8 if (length <= THRESHOLD) { return computeSequentially(); #9 } ForkJoinSumCalculator leftTask = new ForkJoinSumCalculator(numbers, start, start + length/2); #10 leftTask.fork(); #11 ForkJoinSumCalculator rightTask = new ForkJoinSumCalculator(numbers, start + length/2, end); #12 Long rightResult = rightTask.compute(); #13 Long leftResult = leftTask.join(); #14 return leftResult + rightResult; #15 } private long computeSequentially() { #16 long sum = 0; for (int i = start; i < end; i++) { sum += numbers[i]; } return sum; } } #1: Extend RecursiveTask to create a task usable with the fork/join framework. #2: The array of numbers to be summed. #3: The initial and final positions of the portion of the array processed by this subtask. #4: The size of the array under which this task is no longer split into subtasks. #5: Public constructor used to create the main task. #6: Private constructor used to recursively create subtasks of the main task. #7: Override the abstract method of RecursiveTask. #8: The size of the portion of the array summed by this task. #9: If the size is less than or equal to the threshold, compute the result sequentially. #10: Create a subtask to sum the first half of the array. #11: Asynchronously execute the newly created subtask using another thread of the ForkJoinPool. #12: Create a subtask to sum the second half of the array. #13: Execute this second subtask synchronously, potentially allowing further recursive splits. #14: Read the result of the first subtask or wait for it if it isn’t ready. #15: The result of this task is the combination of the results of the two subtasks. #16: Simple algorithm calculating the result of a subtask when it’s no longer divisible.