Difference Between Fork/Join Framework and ExecutorService in Java
The Fork/Join framework provides fine-grained task execution framework with high performance for Java data parallelism. Its parallel computing engine is used by many higher-level frameworks. The fork/join framework supports a style of parallel programming that solves problems by “Divide and conquer”, in the following manner as shown below:
- Splitting a task into sub-tasks.
- Solving sub-tasks in parallel
- Sub-tasks can run in parallel on different cores.
- Sub-tasks can also run concurrently in different threads on a single core.
- Waiting for them to complete
- join() waits for a sub-task to finish
- Merging the results.
- A task uses calls to join() to merge the sub-task results together.
If a task does not return a result then it just waits for its sub-tasks to complete.
Below is a Java program to demonstrate the working of Fork/Join Framework :
6 found 3 times
Now dwelling onto The Java ExecutorService interface extends Executor so we get the one and only execute(Runnable) method defined by Executor. There are a lot more methods in Java ExecutorService compared to Java Executor. Some of the methods in the ExecutorService interface can be used to submit one or more tasks and returns something called a future(essentially a proxy to the result of a computation that runs concurrently and or asynchronously in the background).
The ExecutorService works in the following manner as follows:
- Submit 1+ tasks and return futures for these tasks.
- Manage the lifecycle of tasks and executor service itself, e.g., interrupts worker threads in a pool.
- An ExecutorService instance can be in one of three states
- Running: After being created via a factory method.
- Shutting Down: After being shut down gracefully or abruptly.
- Terminated: After all, tasks have completed.
Now finally let us conclude the differences between Fork/Join Framework and ExecutorService which ais as follows:
|The Fork/Join framework in Java 7 is an implementation of the Divide and Conquer algorithm, in which a central ForkJoinPool executes branching ForkJoinTasks.||ExecutorService is an Executor that provides methods to manage the progress-tracking and termination of asynchronous tasks.|
|Fork/Join Framework makes use of Work Stealing Algorithm. In the Fork/Join framework, when a task is waiting for the completion of the sub-tasks it has created using the join operation, the worker thread that is executing that task looks for another task that has not been executed yet and steals them to start their execution.||Unlike Fork/Join Framework, when a task is waiting for the completion of the sub-tasks it has created using the join operation, the worker thread that is executing that waiting task doesn’t look for another task.|
|Fork-join is wonderful for recursive problems, where a task involves running subtasks and then processing their results.||If you try to solve a recursive problem like this using ExecutorService, you end up with threads tied up waiting for other threads to deliver results to them.|
|Fork Join is an implementation of ExecuterService. The main difference is that this implementation creates a DEQUE worker pool.||Executor service creates asked number of thread, and apply a blocking queue to store all the remaining waiting task.|