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 > Java Holiday Calendar 2016 (Day 17): Parallel Streams in Custom Thread Pools

Java Holiday Calendar 2016 (Day 17): Parallel Streams in Custom Thread Pools

Java 8's streams, in theory, make it easy to use parallelism. It doesn't always turn out that way, but you can create custom thread pools to expand your resources.

Per-Åke Minborg user avatar by
Per-Åke Minborg
·
Dec. 17, 16 · Java Zone · Tutorial
Like (6)
Save
Tweet
7.90K Views

Join the DZone community and get the full member experience.

Join For Free


Image title


Today's tip is about executing parallel streams in custom thread pools. One of the main drivers for developing Java 8 streams was their ability to abstract away parallelism. In theory, we could take any stream and make it parallel with the .parallel() method. In reality... not so easy...

One of the caveats with parallel streams is that they, by default, execute on the common Fork Join Pool. So, if there are a lot of things going on in our application, those parallel tasks will compete with all other tasks in the same pool.

This is something that can be fixed pretty easily. A nice thing with all parallel streams is that we can submit the parallel tasks to any Thread Pool, not just the common Thread Pool. In the example below, I am showing how to stream database content in parallel using Speedment, a stream-based ORM tool and runtime. This way, complex database operations may execute much faster, since the tasks can be spread out on several threads.

However, the same principle would work with any type of stream source, such as the ones we get from Java's built-in Collections like List or Set.

Do This:

final ForkJoinPool forkJoinPool = new ForkJoinPool(3);
forkJoinPool.submit(() -> 
    users.stream() 
            .parallel()
            .filter(User.EMAIL.isNotEmpty())
            .forEach(slowOperation()); 
    );
    try {

    forkJoinPool.shutdown();
    forkJoinPool.awaitTermination(1, TimeUnit.HOURS);
} catch (InterruptedException ie) {
    ie.printStackTrace();
} 


This will create a new Fork Join pool with three threads in which the parallel stream will be executed. This way, we limit the number of parallel tasks to three, and they will operate separately from the common Thread Pool. Well, not entirely separately — tasks in all pools compete for the same limited CPU resources. But that is another story...

Follow the Java Holiday Calendar 2016 with small tips and tricks all the way through the winter holiday season.

Stream (computing) Java (programming language) Calendar (Apple)

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • How to Translate Value to Executives Using an Outcome-Driven Mindset
  • ETL/ELT on Kubernetes With Airbyte
  • How Do You Integrate Emissary Ingress With OPA?
  • Creating an Event-Driven Architecture in a Microservices Setting

Comments

Java Partner Resources

X

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