5

What is the functional difference between sequential and parallel stream in terms of Java 1.8, and how the output will be getting affected?

And in which scenarios to choose parallel or sequential stream?

What will be processing method difference for Sequential and Parallel Stream in Java?!!

I have tried below snippet to test it with the small amount of data, I didn't get any exceptional difference in output.!!

ArrayList<Integer> arrayList = new ArrayList<>();
for(int i = 1; i <= 100;i++) arrayList.add(i);

arrayList.stream().filter(l -> l > 90).forEach(l -> System.out.println(l));

arrayList.parallelStream().filter(l -> l > 90).forEach(l -> System.out.println(l));
Ousmane D.
  • 54,915
  • 8
  • 91
  • 126
Hardik Patel
  • 1,033
  • 1
  • 12
  • 16
  • parallelStream helps you distribute your processing across number of cores. You need to check the parallelism on your system: System.setProperty("java.util.concurrent.ForkJoinPool.common.parallelism", "20"). For more information please refer to [link](https://www.javaspecialists.eu/talks/pdfs/2015%20JAX%20Finance%20in%20London,%20UK%20-%20%22Ripping%20Apart%20Java%208%20Parallel%20Streams%22%20by%20Heinz%20Kabutz%20and%20Kirk%20Pepperdine.pdf) – Mohit Sharma Dec 24 '17 at 14:00

5 Answers5

5

For your concrete example, you got lucky not to see any difference (add the loop to 101 so that elements are distributed a bit worse among threads and see the difference) forEach is documented as:

The behavior of this operation is explicitly nondeterministic

So for parallel processing at least, there will be no order - at least in the sense none that you can rely on. There is forEachOrdered that does guarantee the order - in case you need it.

Choosing parallel or sequential is not easy - you should measure, Brian's advices are the best to read here

Eugene
  • 117,005
  • 15
  • 201
  • 306
4

Since you are creating a parallel stream, it is possible for the elements of the stream to be processed by different threads. A parallel stream allows multiple threads to work on sections of a stream independently. The code where you are using parallelStream() illustrates how you can take advantage of multiple cores.

You cannot see a big difference when you are using parallelStream() on 100 elements. You need to have more then that.

Talking about ordering, there are also 2 ways of achieving that, using forEach and forEachOrdered. The difference between them is that forEach will allow any element of a parallel stream to be processed in any order, while forEachOrdered will always process the elements of a parallel stream in the order of their appearance in the original stream. Therefore, in this case, if you leave forEach as it is, there is no guarantee regarding order.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
2

Generally, a parallel stream is basically a stream that partitions its elements into multiple chunks, processing each chunk with a different thread. Therefore, you can automatically partition the workload of a given operation on all the cores of your multicore processor and keep all of them equally busy.

However, it's important to note that just by invoking parallelStream() doesn't necessarily make the stream parallel, in fact, invoking this method might even return a sequential stream rather a parallel one.

as stated in the java doc:

default Stream<E> parallelStream()

Returns a possibly parallel Stream with this collection as its source. It is allowable for this method to return a sequential stream.

Therefore we can conclude it's up to the library to determine whether it's appropriate to utilise multiple threads. in most cases, this will be the case when there is a huge amount of data to process.

as in your case, there seems to be only 100 elements within the ArrayList hence there is no difference whether you utilise parallelStream() or not.

Lastly, but not least I'd always use a sequential stream to process data in a sequential manner except in cases where there is a huge amount of data to process or when you're experiencing performance issues processing data with a sequential stream in which case you can switch to a parallelStream.

Ousmane D.
  • 54,915
  • 8
  • 91
  • 126
  • I guess the *possibly* part is for example for `List.of(T t)` or `Collections.singletonList` they obviously can not benefit from parallel processing... – Eugene Dec 24 '17 at 15:00
  • @Eugene regarding your first comment my answer still stands. as for your second comment, i'd like to see some stats. – Ousmane D. Dec 24 '17 at 15:23
  • exactly, without stats its impossible to say which is faster, but 100 elements the answer is obvious – Eugene Dec 24 '17 at 15:24
  • @Eugene my answer is based on OPs current example which is 100 elements and by all means with such element count the performance difference whether you use parallelStream or sequential is nothing. However, I’d like to be enlightened and see that having 101 elements will cause a performance difference ;) – Ousmane D. Dec 24 '17 at 15:55
  • 1
    I was not meaning a performance issue, I meant a less *better* distribution among threads ( usually which are a power of two ), and as such in the case of a parallel processing, a "faster" visible result of unordered output – Eugene Dec 24 '17 at 19:56
1

The docs of Stream state that parallel is a property of the stream, but don't add much about implementation specification.

The difference is in the execution of the declarative operations on the stream. In most cases, the difference doesn't show unless it matters as far as the result is concerned.

The best explanation of the difference can probably be found in the forEach terminal stream method that you're calling. The docs for Stream.forEach stipulate:

The behavior of this operation is explicitly nondeterministic. For parallel stream pipelines, this operation does not guarantee to respect the encounter order of the stream, as doing so would sacrifice the benefit of parallelism. For any given element, the action may be performed at whatever time and in whatever thread the library chooses. If the action accesses shared state, it is responsible for providing the required synchronization.

In other words, the sequential stream guarantees order at the expense of concurrency. That's just among other things.

ernest_k
  • 44,416
  • 5
  • 53
  • 99
-1

What is the functional difference between sequential and parallel stream in terms of Java

There should not be any misunderstanding: parallel processing is not the same thing as concurrent processing.

And in which scenarios to choose parallel or sequential stream?

Using parallel stream may or may not be beneficial, it really depends upon what you are using for. Sometimes they are faster and sometimes even slower.

There is a saying If you can do something doesn't really means you should DO.

Mehraj Malik
  • 14,872
  • 15
  • 58
  • 85