Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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, e.g. 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.

like image 410
Tranquility Avatar asked Jan 22 '16 11:01

Tranquility


People also ask

What is the difference between future and CompletableFuture?

Future vs CompletableFuture. CompletableFuture is an extension to Java's Future API which was introduced in Java 5. A Future is used as a reference to the result of an asynchronous computation.

Is parallel stream asynchronous?

Parallel and asynchronous programming can be achieved using parallelStream() and CompletableFuture. These concepts are based on Functional programming. If you are not aware of what is functional programming? I would recommend referring this article to understand functional programming first.

What is a CompletableFuture?

What is CompletableFuture? A CompltableFuture is used for asynchronous programming. Asynchronous programming means writing non-blocking code. It runs a task on a separate thread than the main application thread and notifies the main thread about its progress, completion or failure.

What is the disadvantage of parallel stream in Java 8?

Parallel Streams can actually slow you down It breaks them into subproblems which then run on separate threads for processing, these can go to different cores and then get combined when they're done. This all happens under the hood using the fork/join framework.


1 Answers

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 e.g.

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. i.e. every thread is doing the same thing with different data. As you meantion it is also easier to use.

like image 68
Peter Lawrey Avatar answered Oct 02 '22 09:10

Peter Lawrey