Diving Into Java 8's newWorkStealingPools
Like ForkJoinPools, newWorkStealingPools are great for concurrent tasks and work-stealing, but while they're similar, each pool has ideal uses.
Join the DZone community and get the full member experience.
Join For FreeConcurrency was introduced in Java with version 5.0, and it provided a lot of easy-to-use APIs for writing complex, multithreaded code, which used to be a dreaded area in Java programming. In this, the Executor interface provided a replacement to the original:
new Thread(r).start();
Now, this could be done using more feature packed Executer and ExecutorService interfaces. These interfaces provided a mechanism where a developer can focus on the tasks to be performed concurrently, letting these interfaces take care of Thread creation and their lifecycle management. At the core of these interfaces are the Thread pools, which provide a pool of these worker threads that are used to perform tasks. The most common of these pools are:
- newCachedThreadPool()
- newFixedThreadPool(int nThreads)
- newSingleThreadExecutor()
- newScheduledThreadPool(int corePoolSize)
In Java 8, a new type of thread pool is introduced as newWorkStealingPool() to complement the existing ones. Java gave a very succinct definition of this pool as:
“Creates a work-stealing thread pool using all available processors as its target parallelism level.”
Let’s explore this pool in more detail and see what it brings to our development toolbox.
As its name says, it’s based on a work-stealing algorithm, where a task can spawn other, smaller tasks, which are added to queues of parallel processing threads. If one thread has finished its work and has nothing more to do, it can “steal” the work from the other thread’s queue.
But this work-stealing mechanism is already used by ForkJoinPool in Java and is highly useful when your task(s) spawn smaller tasks, which can be proactively picked up by any available thread, reducing the thread idle time. So what's so new in this ExecutorService's work-stealing mechanism?
To find the answer let’s take a look at the source code (shipped with Java 8) of Executors.java:
/**
* Creates a work-stealing thread pool using all
* {@link Runtime#availableProcessors available processors}
* as its target parallelism level.
* @return the newly created thread pool
* @see #newWorkStealingPool(int)
* @since 1.8
*/
public static ExecutorService newWorkStealingPool() {
return new ForkJoinPool(Runtime.getRuntime().availableProcessors(),
ForkJoinPool.defaultForkJoinWorkerThreadFactory,
null, true);
}
As you can see, in a nutshell, this newWorkStealingPool is returning a ForkJoinPool only, but with some preconfigured parameters:
- Runtime.getRuntime().availableProcessors() – This is the number of processors available to the JVM.
- ForkJoinPool.defaultForkJoinWorkerThreadFactory – Default thread factory to return new threads.
- null - Thread.UncaughtExceptionHandler passed as null
- true – This makes it work in aysnc mode and sets the FIFO order for forked tasks, which are never joined from its work queue.
It is shipped with one more constructor, where you can manually pass the level of parallelism instead of relying on the JVM to get the available Processors. Its source code is:
/**
* Creates a thread pool that maintains enough threads to support
* the given parallelism level, and may use multiple queues to
* reduce contention. The parallelism level corresponds to the
* maximum number of threads actively engaged in, or available to
* engage in, task processing. The actual number of threads may
* grow and shrink dynamically. A work-stealing pool makes no
* guarantees about the order in which submitted tasks are
* executed.
*
* @param parallelism the targeted parallelism level
* @return the newly created thread pool
* @throws IllegalArgumentException if {@code parallelism <= 0}
* @since 1.8
*/
public static ExecutorService newWorkStealingPool(int parallelism) {
return new ForkJoinPool(parallelism,
ForkJoinPool.defaultForkJoinWorkerThreadFactory,
null, true);
}
So to conclude, the newWorkStealingPool provided in Java 8 is not new at all — it just provides a level of abstraction over ForkJoinPool. But yes, it definitely reduces the lines of code I need if my requirement is pretty specific to asynchronous bursts of tasks, requiring all available processors as the target parallelism with minimum thread ideal time.
Also, this new pool added one more gray area on when to use ExecutorService and when to use ForkJoinPool. Now both of them have this common work-stealing principle as their backbones and both can be used to optimize big computational recursive tasks. So now we need a few more thought-wrenching reasons while choosing one of them in our applications.
Opinions expressed by DZone contributors are their own.
Comments