Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs

Java Tip 144: When to use ForkJoinPool vs ExecutorService

Know your options for multithreaded programming on the Java platform

  • Print
  • Feedback

Page 4 of 4

The code based on Java 7's ForkJoinPool was 1.5x times faster than the Java 6 code -- a significant performance gain.

Figures 1 and 2 shows the CPU history for each implementation. Note that CPU usage is pretty much the same, even though the ForkJoinPool implementation is faster.

Figure 1. CPU usage for the Java 6 ExecutorService implementation

Figure 2. CPU usage for the Java 7 ForkJoinPool implementation

In conclusion: Fork/Join for recursive programming

While relatively simple, my benchmarks demonstrate that Fork/Join offers serious gains for solving problems that involve recursion. Because recursion is fundamental to parallel programming on multicore platforms (see Resources) Fork/Join is an essential addition to Java platform concurrency. That said, it does not replace the original java.util.concurrency package. As I've demonstrated, ExecutorService continues to be a fine solution for many concurrent programming tasks. In a programming scenario such as the one I set up, where effective recursion is key to processing power, Fork/Join is likely to be the most effective solution.

Learn more

This article has briefly introduced two approaches to Java concurrency and demonstrated each one's applicability to two common program requirements: data collection and search coverage. See the Resources section to learn more about java.util.concurrency and the uses of Fork/Join in Java 7.

About the author

Madalin Ilie is a software development lead at Endava Romania. He has more than five years' programming experience and has worked in a variety of project domains, from mobile development to heavy financial applications.

Read more about Core Java in JavaWorld's Core Java section.

  • Print
  • Feedback

Resources