编程语言
首页 > 编程语言> > Java 8 parallel 性能测试

Java 8 parallel 性能测试

作者:互联网

本机
在这里插入图片描述

package com.zemcho.echo.csdn;

import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

/**
 * @author linliangxuan
 * @date 2021/8/31 16:59
 */
@Slf4j
public class ParallelImprovedResourceTest {

    public static void main(String[] args) throws IOException, InterruptedException {


        new Thread(() -> {

            parallel();

        }).start();


        new Thread(() -> {

            nonParallel();

        }).start();


        Thread.sleep(1000);

    }


    private static void nonParallel() {

        final long currentTimeMillis = System.currentTimeMillis();

        log.info("nonParallel  非并发迭代-------------------- start");

        final IntStream ints = new Random().ints(200, 0, 10000000);
        final List<Integer> collect = ints.boxed().collect(Collectors.toList());

        collect.stream().forEach(i -> sleep());

        log.info("nonParallel  非并发迭代-------------------- end  花费时长 = {}" , System.currentTimeMillis() - currentTimeMillis);

    }

    private static void sleep() {
        try {
            Thread.sleep(30);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    private static void parallel() {

        final long currentTimeMillis = System.currentTimeMillis();
        log.info("parallel 并发迭代  start");

        final IntStream ints = new Random().ints(200, 0, 10000000);
        final List<Integer> collect = ints.boxed().collect(Collectors.toList());

        collect.stream().parallel().forEach(i -> sleep());

        log.info("parallel 并发迭代-------------------- end  花费时长 = {}" , System.currentTimeMillis() - currentTimeMillis);

    }
}

在这里插入图片描述

为什么呢????

标签:Java,测试,final,currentTimeMillis,collect,ints,import,parallel
来源: https://blog.csdn.net/dgutliangxuan/article/details/120318106