Should I always use a parallel stream when possible?

后端 未结 6 959
走了就别回头了
走了就别回头了 2020-11-22 03:06

With Java 8 and lambdas it\'s easy to iterate over collections as streams, and just as easy to use a parallel stream. Two examples from the docs, the second one using parall

6条回答
  •  悲&欢浪女
    2020-11-22 03:34

    Never parallelize an infinite stream with a limit. Here is what happens:

        public static void main(String[] args) {
            // let's count to 1 in parallel
            System.out.println(
                IntStream.iterate(0, i -> i + 1)
                    .parallel()
                    .skip(1)
                    .findFirst()
                    .getAsInt());
        }
    

    Result

        Exception in thread "main" java.lang.OutOfMemoryError
            at ...
            at java.base/java.util.stream.IntPipeline.findFirst(IntPipeline.java:528)
            at InfiniteTest.main(InfiniteTest.java:24)
        Caused by: java.lang.OutOfMemoryError: Java heap space
            at java.base/java.util.stream.SpinedBuffer$OfInt.newArray(SpinedBuffer.java:750)
            at ...
    

    Same if you use .limit(...)

    Explanation here: Java 8, using .parallel in a stream causes OOM error

    Similarly, don't use parallel if the stream is ordered and has much more elements than you want to process, e.g.

    public static void main(String[] args) {
        // let's count to 1 in parallel
        System.out.println(
                IntStream.range(1, 1000_000_000)
                        .parallel()
                        .skip(100)
                        .findFirst()
                        .getAsInt());
    }
    

    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.

提交回复
热议问题