Summary

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:

  • fork: This allows you to create a new child task
  • join: This allows you to wait for the finalization of a child task and get its results

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:

  • The RecursiveAction class, used as the base class for those ForkJoinTasks that don't return a result.
  • The RecursiveTask class, used as the base class for those ForkJoinTasks that return a result.
  • The 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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.188.125.157