自定义parallelStream的thread pool

简介

之前我们讲到parallelStream的底层使用到了ForkJoinPool来提交任务的,默认情况下ForkJoinPool为每一个处理器创建一个线程,parallelStream如果没有特别指明的情况下,都会使用这个共享线程池来提交任务。

那么在特定的情况下,我们想使用自定义的ForkJoinPool该怎么处理呢?

通常操作

假如我们想做一个从1到1000的加法,我们可以用并行stream这样做:

  1. List<Integer> integerList= IntStream.range(1,1000).boxed().collect(Collectors.toList());
  2. ForkJoinPool customThreadPool = new ForkJoinPool(4);
  3. Integer total= integerList.parallelStream().reduce(0, Integer::sum);
  4. log.info("{}",total);

输出结果:


  1. INFO com.flydean.CustThreadPool - 499500

使用自定义ForkJoinPool

上面的例子使用的共享的thread pool。 我们看下怎么使用自定义的thread pool来提交并行stream:

  1. List<Integer> integerList= IntStream.range(1,1000).boxed().collect(Collectors.toList());
  2. ForkJoinPool customThreadPool = new ForkJoinPool(4);
  3. Integer actualTotal = customThreadPool.submit(
  4. () -> integerList.parallelStream().reduce(0, Integer::sum)).get();
  5. log.info("{}",actualTotal);

上面的例子中,我们定义了一个4个线程的ForkJoinPool,并使用它来提交了这个parallelStream。

输出结果:

  1. INFO com.flydean.CustThreadPool - 499500

总结

如果不想使用公共的线程池,则可以使用自定义的ForkJoinPool来提交。

本文的例子https://github.com/ddean2009/learn-java-streams/tree/master/stream-cust-threadpool

欢迎关注我的公众号:程序那些事,更多精彩等着您!

更多内容请访问 www.flydean.com

自定义parallelStream的thread pool的更多相关文章

  1. 通过Thread Pool Executor类解析线程池执行任务的核心流程

    摘要:ThreadPoolExecutor是Java线程池中最核心的类之一,它能够保证线程池按照正常的业务逻辑执行任务,并通过原子方式更新线程池每个阶段的状态. 本文分享自华为云社区<[高并发] ...

  2. Reporting Service 告警"w WARN: Thread pool pressure. Using current thread for a work item"

    如果Reporting Service偶尔出现不可访问或访问出错情况,这种情况一般没有做监控的话,很难捕捉到.出现这种问题,最好检查Reporting Service的日志文件. 今天早上就遇到这样一 ...

  3. The CLR's Thread Pool

    We were unable to locate this content in zh-cn. Here is the same content in en-us. .NET The CLR's Th ...

  4. MySQL thread pool【转】

    本文来自:http://blog.chinaunix.net/uid-26896862-id-3993773.html 刚刚经历了淘宝的双11,真实感受到了紧张的氛围.尽管DB淡定的度过,但是历程中的 ...

  5. worksteal thread pool

    worksteal的场景 对于一个线程池,每个线程有一个队列,想象这种场景,有的线程队列中有大量的比较耗时的任务堆积,而有的线程队列却是空的,现象就是有的线程处于饥饿状态,而有的线程处于消化不良的状态 ...

  6. Improve Scalability With New Thread Pool APIs

    Pooled Threads Improve Scalability With New Thread Pool APIs Robert Saccone Portions of this article ...

  7. CLR thread pool

    Thread Pooling https://msdn.microsoft.com/en-us/library/windows/desktop/ms686756(v=vs.85).aspx Threa ...

  8. MySQL Thread Pool: Problem Definition

    A new thread pool plugin is now a part of the MySQL Enterprise Edition.In this blog we will cover th ...

  9. Thread Pool Engine, and Work-Stealing scheduling algorithm

    http://pages.videotron.com/aminer/threadpool.htm http://pages.videotron.com/aminer/zip/threadpool.zi ...

随机推荐

  1. Java 混淆器

    在脑海中假想一下,在你苦苦经历 81 难,摸爬滚打研制的技术轮子,终于成型得以问世,遂打个 JAR 包投放于万网之中.可是没过几天,同样功能的轮子出现在你的眼前,关键是核心代码都一样,此时你的内心是否 ...

  2. Controller与RestController的区别

    在使用Spring系列的框架的时候,相信几乎所有人都会遇见@Controller与@RestController两个注解,那么这两个注解到底有什么区别? 1. 标记有@RestController的类 ...

  3. Docker常用yml

    GitLib version: '3.1' services: web: image: 'twang2218/gitlab-ce-zh:11.0.5' restart: always hostname ...

  4. UnboundLocalError,探讨Python中的绑定

    绑定 将python闭包之前,先梳理一下闭包中的绑定操作. 先看看2个相关的错误 NameError 和UnboundLocalError When a name is not found at al ...

  5. ClickHouse学习系列之三【配置文件说明】

    背景 最近花了些时间看了下ClickHouse文档,发现它在OLAP方面表现很优异,而且相对也比较轻量和简单,所以准备入门了解下该数据库系统.在介绍了安装和用户权限管理之后,本文对其配置文件做下相关的 ...

  6. Centos网络的配置

                                                                                                        ...

  7. php--phpstorm使用正则匹配批量替换

    1.首先勾选正则规则 如图勾选右侧的Match Case和Regex 2.编写正则规则:无须添加//左右分解符,直接写正则表达式,注意应该转义的部分,需要原封不动替换的部分加上括号 3.编写替换规则: ...

  8. Unity 游戏框架搭建 2019 (二十七、二十八)弃用的代码警告解决&弃用的代码删除

    在前两篇,我们把所有的示例重头到尾整理了一遍. 当前的状态如下: 要做的事情: (完成) 备份:导出文件,并取一个合理的名字. 遗留问题: (完成) 第八个示例与之前的示例代码重复,功能重复. (完成 ...

  9. flask 入门之 logging

    如想看详细说明,请到: 1.https://www.cnblogs.com/yyds/p/6901864.html 2.https://docs.python.org/2/library/loggin ...

  10. 【django基础】django接口 异步ajax请求 导出数据库成excel表(包裹前端后端)

    py文件: from django.utils.http import urlquote from rest_framework.views import APIView from django.sh ...