• java8 新特性parallelStream 修改默认多线程数量


    parallelStream默认使用了fork-join框架,其默认线程数是CPU核心数。

    通过测试实践,发现有两种方法来修改默认的多线程数量:

    1、全局设置

    在运行代码之前,加入如下代码:

    System.setProperty("java.util.concurrent.ForkJoinPool.common.parallelism", "20");

    2、代码块内部设置

    ForkJoinPool forkJoinPool1 = new ForkJoinPool(20);
    ForkJoinTask<Boolean> fs = forkJoinPool.submit(() -> inputStream.allMatch(element -> {
    Thread.sleep(300);
    System.out.println(Thread.currentThread().getName());
    System.out.println("线程数量:" + Thread.activeCount());
    return new Random().nextInt(100) >= 0;
    }));
    try {
    result = fs.get();
    } catch (InterruptedException e) {
    e.printStackTrace();
    } catch (ExecutionException e){
    e.printStackTrace();
    }
    forkJoinPool.shutdown();

    parallelStream对多线程做了部分优化,如果是java7或之前版本还是老老实实用Concurrency。

  • 相关阅读:
    iOS7 自己定义动画跳转
    Android开发之用双缓冲技术绘图
    postgres 使用存储过程批量插入数据
    渗透过程
    python pytesseract使用
    排序算法比较
    python算法
    python中PIL模块
    数字电路复习
    windows服务参考
  • 原文地址:https://www.cnblogs.com/dotafeiying/p/10912428.html
Copyright © 2020-2023  润新知