Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

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

DZone's Guide to

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.

· Java Zone
Free Resource

The single app analytics solutions to take your web and mobile apps to the next level.  Try today!  Brought to you in partnership with CA Technologies


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.

CA App Experience Analytics, a whole new level of visibility. Learn more. Brought to you in partnership with CA Technologies.

Topics:
java ,custom thread pool ,parallelism ,java performance

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}