Joining the results of the tasks

The fork/join framework provides the ability to execute tasks that return a result. This kind of tasks is implemented by the RecursiveTask class. This class extends the ForkJoinTask class and implements the Future interface provided by the Executor framework.

Inside the task, you have to use the structure recommended by the Java API documentation:

    if (problem size > size){       tasks=Divide(task);       execute(tasks);       joinResults()       return result;     } else {       resolve problem;       return result;     } 

If the task has to resolve a problem bigger than a predefined size, you divide the problem into more subtasks and execute those subtasks using the fork/join framework. When they finish their execution, the initiating task ...

Get Java 9 Concurrency Cookbook - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.