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 8 parallel 性能测试

为什么呢????

上一篇:Java的一维数组和二维数组的初始化方法,年薪超过80万!


下一篇:软硬co-design及面向系统设计——谈谈MindSpore和深度学习框架的必由之路