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

Verify, standardize, and correct the Big 4 + more– name, email, phone and global addresses – try our Data Quality APIs now at Melissa Developer Portal!


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.

Developers! Quickly and easily gain access to the tools and information you need! Explore, test and combine our data quality APIs at Melissa Developer Portal – home to tools that save time and boost revenue. Our APIs verify, standardize, and correct the Big 4 + more – name, email, phone and global addresses – to ensure accurate delivery, prevent blacklisting and identify risks in real-time.

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

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}