Friday, September 15, 2023

Difference between Executor Framework and Fork Join Pool in Java?

Java 5 added Executor Framework to provide an 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 a 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 the Executor framework and ForkJoinPool is that the former provides a general-purpose thread pool, while the latter 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 Java 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 join Multithreading and Parallel Computing in Java course on Udemy to learn more about Executor, Fork Join Framework,  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 differences. 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 or following courses to learn more about Executor Framework, Parallel Programming, and ForkJoin Pool in Java. 
  • What is the difference between thread and process in Java? (answer)
  • Top 5 Courses to learn Multithreading and Concurrency in Java (courses)
  • Top 12 Java Concurrency Questions for Experienced Programmers (see here)
  • Difference between multi-threading and multi-tasking in Java? (answer)
  • 133 Core Java Interview Questions from the last 5 years (see here)
  • 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 the start() and run() method of Thread in Java? (answer)
  • Top 50 Java Thread Interview Questions with Answers (list)
  • Top 5 Books to learn Java Concurrency in-depth (books)
  • Top 10 Java Concurrency and multi-threading best practices (article)
  • Top 10 Courses to learn Java in-depth (courses)
  • 10 Courses to Crack Java Interviews for Beginners (courses)
  • Top 5 courses to Learn Java Performance Tuning (courses)
  • What is happens-before in Java Concurrency? (answer)
  • 6 Books to learn Multithreading and Concurrency in Java (books)
  • 10 Advanced Core Java Courses for Experienced programmers (course)

Thanks for reading this article. If you like this article about Executor Framework vs ForkJoinPool in Java then please share it with your friends and colleagues. If you have any suggestions or feedback then please drop a comment. 

P. S. - If you are relatively new in Java and want to learn multithreading concepts looking for some free courses to start with then you can also check out this free Java Multithreading course on Udemy. It is a good free course to learn  Java Multithreading from scratch as well.

And lastly,  which one is your favorite Executor framework or Fork Join Pool? 

2 comments :

Anonymous said...

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

It's ForkJoinPool instead of ForJoinPool :)

Anonymous said...

yes

Post a Comment