DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Related

  • How to Merge HTML Documents in Java
  • The Future of Java and AI: Coding in 2025
  • Tired of Spring Overhead? Try Dropwizard for Your Next Java Microservice
  • Using Python Libraries in Java

Trending

  • Customer 360: Fraud Detection in Fintech With PySpark and ML
  • Mastering Advanced Aggregations in Spark SQL
  • Infrastructure as Code (IaC) Beyond the Basics
  • The Future of Java and AI: Coding in 2025
  1. DZone
  2. Coding
  3. Java
  4. Diving Into Java 8's newWorkStealingPools

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.

By 
Pawan Sant user avatar
Pawan Sant
·
Mar. 29, 17 · Tutorial
Likes (19)
Comment
Save
Tweet
Share
66.0K Views

Join the DZone community and get the full member experience.

Join For Free

Concurrency 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.

Image title

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. 

Java (programming language)

Opinions expressed by DZone contributors are their own.

Related

  • How to Merge HTML Documents in Java
  • The Future of Java and AI: Coding in 2025
  • Tired of Spring Overhead? Try Dropwizard for Your Next Java Microservice
  • Using Python Libraries in Java

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!