Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does simply calling parallelStream run the tasks in parallel?

I've seen a bunch of examples on the internet that, in order to use the streams API to do parallel stuff, just call the .parallelStream() method like this:

mySet
    .parallelStream()
    ... // do my fancy stuff and collect

But in other cases I've seen the parallel stream being used inside a thread pool submition, like this:

ForkJoinPool.commonPool().submit(() -> {
    mySet
        .parallelStream()
        ... // do my fancy stuff and collect
})

Does just calling parallelStream() executes whatever comes next in multiple concurrent threads? Like in some pre-configured thread pool or something. Or do I have to create my threads and then use the parallel stream?

like image 483
Lucas Noetzold Avatar asked Aug 31 '18 20:08

Lucas Noetzold


People also ask

When should you not use parallelStream?

Similarly, don't use parallel if the stream is ordered and has much more elements than you want to process, e.g. This may run much longer because the parallel threads may work on plenty of number ranges instead of the crucial one 0-100, causing this to take very long time.

Is stream foreach parallel?

parallel foreach() Works on multithreading concept: The only difference between stream(). forEach() and parallel foreach() is the multithreading feature given in the parallel forEach(). This is way faster that foreach() and stream.

What is difference between stream and parallelStream?

A sequential stream is executed in a single thread running on one CPU core. The elements in the stream are processed sequentially in a single pass by the stream operations that are executed in the same thread. A parallel stream is executed by different threads, running on multiple CPU cores in a computer.

Does parallel stream improve performance?

Parallel streams performed significantly better than sequential streams when the number of elements was more than 100,000.


1 Answers

Yes parallelStream runs things in parallel.

Usually when there is a long running stream in parallel you do not want to run that on the commonPool because all other parallel streams are using that too.

This btw is an implementation detail, since stream do not specify what pool they will use for parallel processing, but under the current implementation you should not use ForkJoinPool.commonPool(), but instead create a new pool:

ForkJoinPool pool = new ForkJoinPool(2);

pool().submit(() -> {
mySet
    .parallelStream()
    ... // do my fancy stuff and collect
})
like image 164
Eugene Avatar answered Oct 27 '22 00:10

Eugene