Thursday, December 1, 2016

Difference between Executor Framework and ForkJoinPool in Java?

Java 5 added Executor Framework to provide out-of-box thread pool to Java programmers and Java 7 added ForkJoinPool an implementation of ExecutorService which specifically designed to execute ForkJoinTask. The Executor Framework provides several classes e.g. Executor, ExecutorService, and Executors for execution and creating thread pools. It also provides several built-in, ready to use thread pools like a pool of fixed threads, cached thread pool which can expand itself, spawn new threads if required due to heavy load.

The Job of that thread pool is to accept the task and execute if there is free worker thread available, but ForJoinPool is a special kind of thread pool. They use a work-stealing pattern. All threads in a fork-join pool attempt to find and execute tasks submitted to the pool and/or created by other active tasks.

This enables efficient processing when most tasks spawn other subtasks (as do most ForkJoinTasks like recursive actions), as well as when many small tasks are submitted to the pool from external clients.

In short, the main difference between Executor framework and ForkJoinPool is that the former provides a general-purpose thread pool, while later provides a special implementation that uses a work-stealing pattern for efficient processing of ForkJoinTask. Let's see a couple of more differences to answer this question better.

Also, if you are serious about mastering Java multi-threading and concurrency then I also suggest you take a look at the Java Multithreading, Concurrency, and Performance Optimization course by Michael Pogrebinsy on Udemy. It's an advanced course to become an expert in Multithreading, concurrency, and Parallel programming in Java with a strong emphasis on high performance.

ForkJoinPool vs Executor Framework

When you compare ForkJoinPool with Executor Framework, you either compare the ForkJoinPool with ThreadPoolExecutor class or directly with the ExecutorService interface.

In the first paragraph, I have compared the ForkJoinPool with ExecutorService, not let's see some difference between ForkJoinPool and ThreadPoolExecutor class.

1) The main difference between ForkJoinPool and ThreadPoolExecutor is that ForkJoinPool is designed to accept and execute ForkJoinTask, which is a lightweight version of FutureTask, while ThreadPoolExecutor is designed to provide a normal thread pool which executes each submitted task using one of possibly several pooled threads.

2) Another key difference between ThreadPoolExecutor and ForkJoinPool class is that ForkJoinPool uses a work-stealing pattern, which means one thread can also execute a pending task from another thread. This improves efficiency in the case of ForkJoinTask as most of the ForkJoinTask algorithm spawn new tasks.  You can further read Java Concurrency in Practice by Brian Goetz to learn more about work stealing patterns and other parallel computing patterns.

Difference between Executor Framework and ForkJoinPool in Java?

3) Some of the common ThreadPoolExecutor provided by JDK API is SingleThreadExecutor, which is a thread pool of just one background thread, a cached thread pool provided by Executors.newCachedThreadPool, which is a pool of unbounded threads, where the pool can spawn new threads if needed and reclaim old threads when not needed.

The third type of common use of ThreadPoolExecutor is a fixed thread pool, where the pool has a fixed number of threads in its lifetime, it's also an example of a bounded thread pool.

That's all about the difference between ForkJoinPool and Executor Framework in Java. You should use ForkJoinPool if you are using that framework and submit ForkJoinTask, otherwise just use a ThreadPoolExecutor instance, provided by various factory methods of Executors class e.g. Executors.newSingleThreadPoolExecutor(), Executors.newCachedThreadPoolExecutor() and Executors.newFixedThreadPoolExecutor() class.

Let me know if you come across any other useful difference. If you want to learn more about how to use the ForkJoin pool efficiently, I would suggest reading Java Concurrency in Practice by Brian Goetz, one of the must-read books for any advanced Java developer.
  • What is the difference between thread and process in Java? (answer)
  • Difference between multi-threading and multi-tasking in Java? (answer)
  • The difference between synchronized and ReentrantLock in Java? (answer)
  • How volatile keyword works in Java? (answer)
  • What is the difference between CyclicBarrier and CountDownLatch in Java? (answer)
  • Difference between extends Thread and implements Runnable in Java? (answer)
  • Difference between start() and run() method of Thread in Java? (answer)

1 comment :

Anonymous said...

Good Article. Please do correction here : but ForJoinPool is a special kind of thread pool.

It's ForkJoinPool instead of ForJoinPool :)

Post a Comment