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
//可见并行流比顺序流效率高几乎六倍
}
}
网友评论