Stream
returned by map
or mapToObj
methods is always sequential or does it depend on whether the state of the calling stream was parallel or not?
The documentation of IntStream
does not answer this explicitly or I cannot understand it properly:
I am wondering if my stream from the following example will be parallel up to the end or it will change at some point.
IntStream.range(1, array_of_X.size())
.parallel()
.mapToObj (index -> array_of_X.get(index)) // mapping#1
.filter (filter_X)
.map (X_to_Y) //mapping#2
.filter (filter_Y)
.mapToInt (obj_Y_to_int) //mapping#3
.collect(value -> Collectors.summingInt(value));
Sequential Streams. By default, any stream operation in Java is processed sequentially, unless explicitly specified as parallel.
Parallel streams divide the provided task into many and run them in different threads, utilizing multiple cores of the computer. On the other hand sequential streams work just like for-loop using a single core.
parallelStream() streams are always parallel... And then there is Spliterator , and in particular its . characteristics() , one of them being that it can be CONCURRENT , or even IMMUTABLE .
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.
No, it will never change (unless you explicitely change it yourself).
What you have written corresponds to a Stream pipeline and a single pipeline has a single orientation: parallel or sequential. So there is no "parallel up to the end" because either the whole pipeline will be executed in parallel or it will be executed sequentially.
Quoting the Stream package Javadoc:
The only difference between the serial and parallel versions of this example is the creation of the initial stream, using "
parallelStream()
" instead of "stream()
". When the terminal operation is initiated, the stream pipeline is executed sequentially or in parallel depending on the orientation of the stream on which it is invoked. Whether a stream will execute in serial or parallel can be determined with theisParallel()
method, and the orientation of a stream can be modified with theBaseStream.sequential()
andBaseStream.parallel()
operations. When the terminal operation is initiated, the stream pipeline is executed sequentially or in parallel depending on the mode of the stream on which it is invoked.
This means that the only way for a Stream pipeline to change its orientation is by calling one of sequential()
or parallel()
method. Since this is global to the Stream API, this is not written for every operation but in the package Javadoc instead.
With the code in your question, the Stream pipeline will be executed in parallel because you explicitely changed the Stream orientation by invoking parallel()
.
It is important to note that the resulting orentiation of the Stream will be the last call made to parallel()
or sequential()
. Consider the three following examples:
public static void main(String[] args) {
System.out.println(IntStream.range(0, 10).isParallel());
System.out.println(IntStream.range(0, 10).parallel().isParallel());
System.out.println(IntStream.range(0, 10).parallel().map(i -> 2*i).sequential().isParallel());
}
false
since IntStream.range
returns a sequential Streamtrue
since we invoked parallel()
false
since, even if we call parallel()
in the pipeline, we invoked sequential()
afterwards so it reset the total orientation of the Stream pipeline to serial.Note that, still quoting:
The stream implementations in the JDK create serial streams unless parallelism is explicitly requested.
so every Stream you are going to retrieve will be sequential unless you explicitely requested a parallel Stream.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With