The divide and conquer design technique is a very popular approach to solve different kinds of problems. You divide the original problem into smaller problems and those problems into smaller ones until we have enough simple problems to solve it directly. In version 7, the Java concurrency API introduced a special kind of Executor
optimized for these kinds of problems. It's the Fork/Join Framework. It's based on the following two operations:
Using those operations, Fork/Join tasks have the following appearance:
if ( problem.size() > DEFAULT_SIZE) { childTask1=new Task(); childTask2=new Task(); childTask1.fork(); childTask2.fork(); childTaskResults1=childTask1.join(); childTaskResults2=childTask2.join(); taskResults=makeResults(childTaskResults1, childTaskResults2); return taskResults; } else { taskResults=solveBasicProblem(); return taskResults; }
In this chapter, you have solved three different problems using the Fork/Join framework such as the k-means clustering algorithm, a data filtering algorithm, and the merge sort algorithm.
You have used default ForkJoinPool
, provided by the API (this is a new feature of the Java 8 version), and created a new ForkJoinPool
object. You have also used the three types of ForkJoinTask
s
:
RecursiveAction
class, used as the base class for those ForkJoinTasks
that don't return a result.RecursiveTask
class, used as the base class for those ForkJoinTasks
that return a result.CountedCompleter
class, introduced in Java 8 and used as the base class for those ForkJoinTasks
that need to execute a method or launch another task when all its child subtasks finish their execution.In the next chapter, you will learn how to use the MapReduce programming technique using the new Java 8 parallel streams to get the best performance processing very big datasets.
18.188.125.157