自定义parallelStream的thread pool

简介

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

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

通常操作

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

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

输出结果:


INFO com.flydean.CustThreadPool - 499500

使用自定义ForkJoinPool

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

List<Integer> integerList= IntStream.range(1,1000).boxed().collect(Collectors.toList());

ForkJoinPool customThreadPool = new ForkJoinPool(4);
Integer actualTotal = customThreadPool.submit(
() -> integerList.parallelStream().reduce(0, Integer::sum)).get();
log.info("{}",actualTotal);

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

输出结果:

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. C 苟富贵

    时间限制 : 15000 MS   空间限制 : 524288 KB 问题描述 你最近买六合彩赚了很多钱,导致一个银行账户存不下了,于是你开设了 N 个账户,第 i 个账户里存有 Ai 元. 你的好友 ...

  2. ERROR:TypeError: Cannot read property 'upgrade' of undefined

  3. python--Django(三)视图

    Django的视图 不同于其他语言的MVC模式,Django采用的是MVT模式,即Model.View.Template,这里的View其实质就是其他语言中的Controller(emmm.....) ...

  4. abp(net core)+easyui+efcore实现仓储管理系统——入库管理之八(四十四)

    abp(net core)+easyui+efcore实现仓储管理系统目录 abp(net core)+easyui+efcore实现仓储管理系统——ABP总体介绍(一) abp(net core)+ ...

  5. JS入门系列(1)-原型-函数原型

    实例1: 首先定义一个Persion类或者说是函数 var p1 = Persion();:表示,作为普通函数调用 var p2 = new Persion();:表示,作为构造器调用 创建函数之后, ...

  6. python3(六) for while

    # Python的循环有两种,一种是for...in循环,依次把list或tuple中的每个元素迭代出来 names = ['Michael', 'Bob', 'Tracy'] for name in ...

  7. JNDI数据源的配置及使用 (2010-11-21 21:16:43)转载▼

    JNDI数据源的配置及使用 (2010-11-21 21:16:43)转载▼ 标签: 杂谈 分类: 数据库 数据源的作用 JDBC操作的步骤: 1. 加载驱动程序 2. 连接数据库 3. 操作数据库 ...

  8. java 代码执行cmd 返回值异常 (关于JAVA Project.waitfor()返回值是1)

    关于JAVA Project.waitfor()返回值是1   0条评论 Project.waitfor()返回值是1,找了很久从网上没有发现关于1的说明. 这时对源代码调试了一下,发现Project ...

  9. JVM崩溃的原因及解决!

    JVM崩溃的原因及解决! 前些天,搞JNI的时候,报了个JVM崩溃的错.错误信息如下: # # An unexpected error has been detected by HotSpot Vir ...

  10. DES原理及代码实现

    一.DES基础知识DES技术特点 DES是一种用56位密钥来加密64位数据的方法    DES采取了分组加密算法:明文和密文为64位分组长度    DES采取了对称算法:加密和解密除密钥编排不同外,使 ...