Java教程

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 性能测试的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!