Java8 Stream 的并发

先看一下下面的程序

public class Main
{
  public static void main(String[] args){
    long startTime=System.currentTimeMillis();   //获取开始时间     LongStream.rangeClosed(1L, 10000L)
    // .parallel()     .forEach(in->{
      Thread thread = Thread.currentThread();
      System.out.println("thread: "+thread.getName()+", value: "+in);
      try{
        Thread.sleep(5);
      }catch (Exception ex){
        ex.printStackTrace();
      }
    });
    long endTime=System.currentTimeMillis(); //获取结束时间     System.out.println("程序运行时间: "+(endTime-startTime)+"ms");

  }
}

结果

thread: main, value: 1
thread: main, value: 2
thread: main, value: 3
thread: main, value: 4
thread: main, value: 5
thread: main, value: 6
thread: main, value: 7
thread: main, value: 8
thread: main, value: 9
thread: main, value: 10
thread: main, value: 11
thread: main, value: 12
thread: main, value: 13
thread: main, value: 14
thread: main, value: 15
thread: main, value: 16
thread: main, value: 17
thread: main, value: 18
thread: main, value: 19
thread: main, value: 20
thread: main, value: 21
thread: main, value: 22
thread: main, value: 23
thread: main, value: 24
thread: main, value: 25
...
程序运行时间: 51166ms

可以看出这个和程序串行执行。

然后我们修改一下程序,添加了1个parrallel,如下

public class Main
{
  public static void main(String[] args){
    long startTime=System.currentTimeMillis();   //获取开始时间     LongStream.rangeClosed(1L, 10000L)
    .parallel()
    .forEach(in->{
      Thread thread = Thread.currentThread();
      System.out.println("thread: "+thread.getName()+", value: "+in);
      try{
        Thread.sleep(5);
      }catch (Exception ex){
        ex.printStackTrace();
      }
    });
    long endTime=System.currentTimeMillis(); //获取结束时间     System.out.println("程序运行时间: "+(endTime-startTime)+"ms");

  }
}

运行结果为:

thread: main, value: 17968751
thread: ForkJoinPool.commonPool-worker-4, value: 28222657
thread: ForkJoinPool.commonPool-worker-2, value: 37194824
thread: ForkJoinPool.commonPool-worker-5, value: 9375001
thread: ForkJoinPool.commonPool-worker-1, value: 6250001
thread: ForkJoinPool.commonPool-worker-6, value: 1
thread: ForkJoinPool.commonPool-worker-3, value: 3125001
thread: ForkJoinPool.commonPool-worker-7, value: 51914786
thread: main, value: 17968752
thread: ForkJoinPool.commonPool-worker-2, value: 37194825
thread: ForkJoinPool.commonPool-worker-4, value: 28222658
thread: ForkJoinPool.commonPool-worker-1, value: 6250002
thread: ForkJoinPool.commonPool-worker-7, value: 51914787
thread: ForkJoinPool.commonPool-worker-5, value: 9375002
thread: ForkJoinPool.commonPool-worker-6, value: 2
thread: ForkJoinPool.commonPool-worker-3, value: 3125002
thread: ForkJoinPool.commonPool-worker-2, value: 37194826
thread: ForkJoinPool.commonPool-worker-1, value: 6250003
thread: ForkJoinPool.commonPool-worker-4, value: 28222659
thread: main, value: 17968753
thread: ForkJoinPool.commonPool-worker-7, value: 51914788
thread: ForkJoinPool.commonPool-worker-5, value: 9375003
thread: ForkJoinPool.commonPool-worker-3, value: 3125003
thread: ForkJoinPool.commonPool-worker-6, value: 3
thread: ForkJoinPool.commonPool-worker-2, value: 37194827
thread: ForkJoinPool.commonPool-worker-1, value: 6250004
thread: ForkJoinPool.commonPool-worker-4, value: 28222660
thread: main, value: 17968754
...
程序运行时间: 6459ms

从时间上来看,并发执行只需要要6s,而串行需要51s。从结果来看,串行能保证输出的顺序性,并行不行。从内存利用率来看,并发内存占用高。如果你都CPU核更多,将会有更多的线程参与运算,进一步加快计算,符合现代的多核计算思想。

有兴趣的同学可以把睡眠的代码去掉,看看cpu使用率,并发情况下cpu使用率比较均匀。

    原文作者:me liar
    原文地址: https://zhuanlan.zhihu.com/p/24060979
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞