美文网首页
并行流和串行流

并行流和串行流

作者: YNZXGWZM | 来源:发表于2018-08-02 17:40 被阅读0次
    package com.mc.day1.lambda;
    
    import org.junit.Test;
    
    import java.time.Duration;
    import java.time.Instant;
    import java.util.stream.LongStream;
    
    public class ForkJoin {
    
        @Test
        public void test(){
         //测试并行流  parallel()  并行流和顺序流 sequential
            Instant now = Instant.now();
            long reduce = LongStream.rangeClosed(0, 1000000000L).parallel().reduce(0, Long::sum);
            Instant end = Instant.now();
            System.out.println(Duration.between(now,end).toMillis());
            //361ms
    
    
            //一般的没有用到并行流
            Instant now1 = Instant.now();
            long reduce1 = LongStream.rangeClosed(0, 1000000000L).sequential().reduce(0, Long::sum);
            Instant end1 = Instant.now();
            System.out.println(Duration.between(now1,end1).toMillis());
            //1889ms
            //可见并行流比顺序流效率高几乎六倍
    
        }
    }
    

    相关文章

      网友评论

          本文标题:并行流和串行流

          本文链接:https://www.haomeiwen.com/subject/vrusvftx.html