DZone
Java Zone
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
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Java Zone > Controlling the Parallelism Level of Java Parallel Streams

Controlling the Parallelism Level of Java Parallel Streams

If you want more control over your streams' levels of parallelism, you can't just call parallel(). Here's how to assume programmatic control.

Kamil Szymański user avatar by
Kamil Szymański
·
Oct. 18, 17 · Java Zone · Tutorial
Like (9)
Save
Tweet
10.10K Views

Join the DZone community and get the full member experience.

Join For Free

With the recent Java 9 release, we got many new goodies to play with to improve our solutions — once we grasp those new features. The release of Java 9 is also a good time to revise whether we have grasped Java 8's features.

In this post, I’d like to bust the most common misconception about Java parallel streams. It’s often said that you cannot control parallel streams’ parallelism level in a programmatic way, that parallel streams always run on a shared ForkJoinPool.commonPool() and there’s nothing you can do about it.

This is the case if you make your stream parallel by just adding a parallel() call to the call chain. That might be sufficient in some cases, e.g. if you perform only lightweight operations on that stream, but if you need to gain more control over your stream’s parallel execution, you need to do a bit more than just calling parallel().

Instead of diving in into theory and technicalities, let’s jump straight to the self-documenting example.

Having a parallel stream being processed on shared ForkJoinPool.commonPool():

Set<FormattedMessage> formatMessages(Set<RawMessage> messages) {
    return messages.stream()
            .parallel()
            .map(MessageFormatter::format)
            .collect(toSet());
}

Let’s move the parallel processing to a pool that we can control and don’t have to share:

private static final int PARALLELISM_LEVEL = 8;

Set<FormattedMessage> formatMessages(Set<RawMessage> messages) {
    ForkJoinPool forkJoinPool = new ForkJoinPool(PARALLELISM_LEVEL);
    try {
        return forkJoinPool.submit(() -> formatMessagesInParallel(messages))
                .get();
    } catch (InterruptedException | ExecutionException e) {
        // handle exceptions
    } finally {
        forkJoinPool.shutdown();
    }
}

private Set<FormattedMessage> formatMessagesInParallel(Set<RawMessage> messages) {
    return messages.stream()
            .parallel()
            .map(MessageFormatter::format)
            .collect(toSet());
}

In this example, we’re interested only in the parallelism level of the ForkJoinPool, though we can also control ThreadFactory and UncaughtExceptionHandler if needed.

Under the hood, the ForkJoinPool scheduler will take care of everything, including incorporating the work-stealing algorithm to improve parallel processing efficiency. Having said that, it’s worth mentioning that manual processing using ThreadPoolExecutor might be more efficient in some cases, e.g. if the workload is evenly distributed over worker threads.

Stream (computing) Java (programming language)

Published at DZone with permission of Kamil Szymański, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Message Queuing and the Database: Solving the Dual Write Problem
  • 3 Predictions About How Technology Businesses Will Change In 10 Years
  • Use Lambda Function URL To Write a Serverless App Backed by DynamoDB
  • An Introduction to Graph Data

Comments

Java Partner Resources

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo