Stream的顺序流与并行流

/**
 * @auther hhh
 * @date 2019/1/2 22:52
 * @description
 */
public class StreamAPI2 {
    /**
     * 流的特性:支持并行流与顺序流
     * 并行流:多个线程同时运行
     * 顺序流:使用主线程,单线程
     */
    public static void main(String[] args) {
        Optional<Integer> optionalInteger = Stream.iterate(1, x -> x+1).limit(200).peek(x->{
                    System.out.println(Thread.currentThread().getName());
                }
        ).max(Integer::compareTo);//输出 main main Optional[200],始终使用的是主线程,说明流默认是顺序流,使用的是主线程
        System.out.println(optionalInteger);
        Optional<Integer> optional = Stream.iterate(1, x -> x+1).limit(200).peek(x->{
                    System.out.println(Thread.currentThread().getName());
                }
        ).parallel().max(Integer::compareTo);//加上 .parallel()可以将其修改成并行流,内部以多线程并行执行任务的方式执行
        //输出:说明有多个线程在并行执行
        //ForkJoinPool.commonPool-worker-2
        //ForkJoinPool.commonPool-worker-3
        //Optional[200]
        System.out.println(optional);


        //将并行流变成顺序流 加上.sequential()
        Optional<Integer> optional1 = Stream.iterate(1, x -> x+1).limit(200).peek(x->{
                    System.out.println(Thread.currentThread().getName());
                }
        ).parallel().sequential().max(Integer::compareTo);
        //设置lambda表达式并行的线程数量,使用parallelism
        //设置启动变量:加上这个参数设置 java.util.concurrent.ForkJoinPool.common.parallelism
        //设置为5个线程数量
        System.setProperty("java.util.concurrent.ForkJoinPool.common.parallelism","5");
    }
}

 

posted @ 2019-01-02 23:15  huanghaunghui  阅读(4131)  评论(0编辑  收藏  举报