Parallel programming was possible in Java only from Java 7 with the advent of Join/Fork framework.
Parallel programming exists in java since early versions. It was enhanced with Java 5 java.util.concurrent package classes and Java 7 ForkJoinPool further enhanced parallel programming to new level.
You can find answer to your questions in this article by oracle.
Java Platform, Standard Edition (Java SE) 5 and then Java SE 6 introduced a set of packages providing powerful concurrency building blocks. Java SE 7 further enhanced them by adding support for parallelism
Example of divide and conquer problem:
Find sum of integers in large array
Instead of sequentially computing the sum, divide array into multiple partitions and assign computation task on each partition to different task.
The problem with the executors for implementing divide and conquer algorithms is not related to creating subtasks, because a Callable
is free to submit a new subtask to its executor and wait for its result in a synchronous or asynchronous fashion.
The issue is that of parallelism: When a Callable
waits for the result of another Callable
, it is put in a waiting state, thus wasting an opportunity to handle another Callable
queued for execution.
The fork/join framework added to the java.util.concurrent package in Java SE 7
Additions for Supporting Parallelism:
The core addition is a new ForkJoinPool
executor that is dedicated to running instances implementing ForkJoinTask. ForkJoinTask
objects support the creation of subtasks plus waiting for the subtasks to complete. With those clear semantics, the executor is able to dispatch tasks among its internal threads pool by “stealing” jobs when a task is waiting for another task to complete and there are pending tasks to be run.
ForkJoinTask objects feature two specific methods:
The fork()
method allows a ForkJoinTask
to be planned for asynchronous execution. This allows a new ForkJoinTask
to be launched from an existing one.
In turn, the join()
method allows a ForkJoinTask
to wait for the completion of another one.