自定义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. 【tensorflow2.0】AutoGraph的机制原理

    有三种计算图的构建方式:静态计算图,动态计算图,以及Autograph. TensorFlow 2.0主要使用的是动态计算图和Autograph. 动态计算图易于调试,编码效率较高,但执行效率偏低. ...

  2. Redis 笔记(三)—— LIST 常用命令

    常用命令 命令 用例和描述 RPUSH RPUSH key value [value ...] —— 将一个或多个值推入列表的右端 LPUSH LPUSH key value [value ...] ...

  3. docker 服务器安装harbor

    一.Harbor是什么? 二.环境搭建 2.1在linux centos搭建服务 2.2docker安装 yum安装 yum install docker 卸载 :pip uninstall dock ...

  4. Java第七天,类的继承

    面向对象编程的三大特征: 封装.继承.多态 今天我们学习继承! 继承是多态的前提,如果没有继承就没有多态. 继承主要解决的问题就是共性抽取(将许多类共有的特性便作父类,这样可以较大程度的优化代码). ...

  5. Python爬虫利器 cURL你用过吗?

    hello,小伙伴们,今天给大家分享的开源项目是一个python爬虫利器,感兴趣的小伙伴看完这篇文章不妨去尝试一下,这个开源项目就是curlconverter,不知道小伙伴们分析完整个网站后去code ...

  6. 利用xposed hook Auto.js程序、解密其js脚本

    一.原理 原理很简单就是hook auto.js的com.stardust.autojs.script.StringScriptSource类,当然前题你要逆向的auto.js程序dex没有加固,当然 ...

  7. 2020-3-30 20175110王礼博 Exp3 免杀原理与实践

    目录 1.正确使用msf编码器 2.msfvenom生成如jar之类的其他文件 3.veil 4.加壳工具 5.使用C + shellcode编程 6.使用其他课堂未介绍方法 7.通过组合应用各种技术 ...

  8. 记一次Windows蓝屏分析

    大半夜收到此类信息,应该是让所有系统管理员最头大的事情了 首先我快速通过iDRAC,发现服务器发生了重启操作,并得到相关日志信息 通过Dell的官方解释,确定了该问题是OS层面的异常导致.打开Wind ...

  9. 数据结构和算法(Golang实现)(6)简单入门Golang-并发、协程和信道

    并发.协程和信道 Golang语言提供了go关键字,以及名为chan的数据类型,以及一些标准库的并发锁等,我们将会简单介绍一下并发的一些概念,然后学习这些Golang特征知识. 一.并发介绍 我们写程 ...

  10. AJ学IOS(49)多线程网络之线程的创建NSThreand

    AJ分享,必须精品 一:NSThread的基本使用 1:创建和启动线程 一个NSThread对象就代表一条线程 创建.启动线程 NSThread *thread = [[NSThread alloc] ...