简体   繁体   中英

Difference between parallel stream and CompletableFuture

In the book "Java 8 in action" (by Urma, Fusco and Mycroft) they highlight that parallel streams internally use the common fork join pool and that whilst this can be configured globally, eg using System.setProperty(...), that it is not possibly to specify a value for a single parallel stream.

I have since seen the workaround that involves running the parallel stream inside a custom made ForkJoinPool.

Later on in the book, they have an entire chapter dedicated to CompletableFuture, during which they have a case study where they compare the respective performance of using a parallelStream VS a CompletableFuture. It turns out their performance is very similar - they highlight the reason for this as being that they are both as default using the same common pool (and therefore the same amount of threads).

They go on to show a solution and argue that the CompletableFuture is better in this circumstance as it can be congifured to use a custom Executor, with a thread pool size of the user's choice. When they update the solution to utilise this, the performance is significantly improved.

This made me think - if one were to do the same for the parallel stream version using the workaround highlighted above, would the performance benefits be similar, and would the two approaches therefore become similar again in terms of performance? In this case, why would one choose the CompletableFuture over the parallel stream when it clearly takes more work on the developer's part.

In this case, why would one choose the CompletableFuture over the parallel stream when it clearly takes more work on the developer's part.

IMHO This depends on the interface you are looking to support. If you are looking to support an asynchronous API eg

CompletableFuture<String> downloadHttp(URL url);

In this case, only a completable future makes sense because you may want to do something else unrelated while you wait for the data to come down.

On the other hand parallelStream() is best for CPU bound tasks where you want every tasks to perform a portion of some work. ie every thread is doing the same thing with different data. As you meantion it is also easier to use.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM