Java 5 and Java 6 added several concurrency constructs to the java.util.concurrent package to help developers deal with problems involving advanced level of concurrency. The executor framework was one of the several great additions in Java 5. Java 7 added a new implementation of the executor service, which is known as a fork/join framework. The focus of the fork/join framework is to solve those problems efficiently, which may use the divide-and-conquer algorithm, by taking advantage of the multiple processors on a machine. Before Java 7, we had several Java constructs to help us solve the problems that involved concurrency. The fork/join framework helps us solve the problems that involve parallelism. Typically, the fork/join framework is suitable in a situation where:
- A task can be divided in multiple subtasks that can be executed in parallel.
- When subtasks are finished, the partial results can be combined to get the final result.
The fork/join framework creates a pool of threads to execute the subtasks. When a thread is waiting on a subtask to finish, the fork/join framework uses that thread to execute other pending subtasks of other threads. The technique of an idle thread executing other thread’s task is called work stealing. The fork/join framework uses a work-stealing algorithm to enhance the performance.
Click here to read the complete post.