1. .parallel() and .sequential()
Stream API has the .parallel() function. It offers the parallel attrbute to the stream literally. You can use this function at the mid place and the calculations will be divided after calling this function. The below is the example how to add from 1 to n using the parallel stream.
1 |
|
You can change the type between sequential and parallel in the one stream either like the below.
1 |
|
2. the sort of overheads of the stream technique.
If you are the novice of the parallel programming or stream thing then you may misunderstand about what these new techniques are always better then the sequential or conventional things. but it’s not truth. Because new techniques must have the additional task. for example the context switching or creating the new thread or translating the object type to primitive type, it’s called boxing and unboxing. so You have to consider about the overhead of new techniques when you use that.
boxing and unboxing
Look at the below the code.
1 |
|
It’s the calculation using the sequential stream. but if you know the boxing and unboxing, You can upgrade your code more effectively. the method reference ‘Long::sum’ in the reduce() function needs the object type of long. It’s not a primitive type but the source and the result of the stream is the primitive type of long. so the above code must always translate the type between object and primitive of long. Actually It’s really high cost. so the below is more faster than the above thing.
1 |
|
Change the from Stream to LongStream. It’s for the primitive type of long. then you don’t need to translate betweens the types.
Shared Variable.
like the public toilet of which someone use that then the others can’t use the toilet, the shared variables can be approached only one thread at a time for keeping the MUTEX status. so the design needed the shared variable isn’t well to the parallel technique. if you use just like that, It’s more worse than sequential because of the overhead like the context swiching. the below code is the conventional design using the shared variable and the second thing is the same code using the stream.
1 |
|
1 |
|
They aren’t useful code from the parallel perspective. iterate() function of the stream use the shared variables internally either. so when you use the parallel stream you should avoid the iterate() function and use the rangeClosed() function.
1 |
|
rangeClosed() function can divide the stream itself. and the divided streams are so useful to the parallel technique.