Benefit of using forEachOrdered with Parallel streams

后端 未结 3 1496
谎友^
谎友^ 2020-12-24 15:05

The official Oracle documentation says:

Note that you may lose the benefits of parallelism if you use operations like forEachOrdered with parallel s

3条回答
  •  春和景丽
    2020-12-24 15:57

    I found stream().forEachOrdered() is ~50% faster than its parallel counterpart. Plus the parallel one uses at least one thread from common fork-join thread pool, which is - one less thread for other parallel streams running in JVM.

    public static void main(String[] args) { long start = System.currentTimeMillis(); IntStream.range(0,10000000).parallel().forEachOrdered(i -> { //System.out.println(Thread.currentThread().getName()); int p = 1 * 1 ; }); System.out.println((System.currentTimeMillis() - start)); }

提交回复
热议问题