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

前端 未结 1 932
粉色の甜心
粉色の甜心 2020-12-06 22:27

In the book Java 8 In Action, section 7.1.1, the authors state that a stream can benefit from parallel processing by adding the function .parallel().

相关标签:
1条回答
  • 2020-12-06 23:26

    Here you create an infinite stream and limit it afterwards. There are known problems about processing infinite streams in parallel. In particular there's no way to split the task to equal parts effectively. Internally some heuristics are used which are not well suitable for every task. In your case it's much better to create the finite stream using LongStream.range:

    import java.util.stream.LongStream;
    
    public class ParallelPlay {
    
        public static void main(String[] args) {
            System.out.println(parallelSum(100_000_000));
        }
    
        public static long parallelSum(long n) {
            return LongStream.rangeClosed(1, n).parallel().sum();
        }
    }
    

    In this case Stream engine knows from the very beginning how many elements you have, so it can split the task effectively. Also note that using the LongStream is more effecient as you will have no unnecessary boxing.

    In general avoid infinite streams if you can solve your task with finite ones.

    0 讨论(0)
提交回复
热议问题