本机
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);
}
}