Parallelism and plan view in Java 8 streams

Hey, I have a problem with parallelism when I use flatmap

IntStream.of(-1,1).parallel().flatMap(i->IntStream.range(0,1000).parallel()).forEach(System.out::println);

Set internal flag to parallel? If I leave it, the results look similar Why is the code (referencepipeline) mapped sequentially? I feel confused: @ h_ 403_ 8@

result.sequential().forEach(downstream);

http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/8-b132/java/util/stream/ReferencePipeline.java#ReferencePipeline.flatMap%28java.util.function.Function%29 @H_ 403_ 8@

Solution

In the current JDK (jdk1.8.0_25), the answer is No. It doesn't matter to set the internal flag to parallel,

result.sequential().forEach(downstream);

("result" is an internal flow. The DOC of its sequential () method says: it returns an equivalent flow, which is sequential. It may return itself, because the flow is already sequential, or because the underlying flow state is modified to sequential@ H_ 403_ 8@

In most cases, it is impossible to make the internal flow parallel; If the external flow has the same number of items as the number of threads that can run in parallel (forkjoinpool. Commonpool() Getparallelism() = 3) in my computer@ H_ 403_ 8@

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>