Java8 并行流

package com.xx;

import org.junit.Test;

import java.time.Duration;
import java.time.Instant;
import java.util.stream.LongStream;

/**
 * @author xx
 * DateTime: 2019/11/20 11:08
 * Description: Java8并行流
 *          并行流:parallel
 *          顺序流:sequential
 */
public class ForkJoinTest {

    /**
     * 并行流
     */
    @Test
    public void test1() {
        Instant start = Instant.now();
        LongStream.range(0, 100000000000L).parallel().reduce(0, Long::sum);

        Instant end = Instant.now();
        // 7338
        System.out.println(Duration.between(start, end).toMillis());
    }


    /**
     * 串行流
     */
    @Test
    public void test2() {
        Instant start = Instant.now();
        LongStream.range(0, 100000000000L).sequential().reduce(0, Long::sum);

        Instant end = Instant.now();
        // 37893
        System.out.println(Duration.between(start, end).toMillis());
    }
}

猜你喜欢

转载自blog.csdn.net/progammer10086/article/details/103159650