自定义parallelStream的thread pool
自定义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的更多相关文章
- 通过Thread Pool Executor类解析线程池执行任务的核心流程
摘要:ThreadPoolExecutor是Java线程池中最核心的类之一,它能够保证线程池按照正常的业务逻辑执行任务,并通过原子方式更新线程池每个阶段的状态. 本文分享自华为云社区<[高并发] ...
- Reporting Service 告警"w WARN: Thread pool pressure. Using current thread for a work item"
如果Reporting Service偶尔出现不可访问或访问出错情况,这种情况一般没有做监控的话,很难捕捉到.出现这种问题,最好检查Reporting Service的日志文件. 今天早上就遇到这样一 ...
- 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 ...
- MySQL thread pool【转】
本文来自:http://blog.chinaunix.net/uid-26896862-id-3993773.html 刚刚经历了淘宝的双11,真实感受到了紧张的氛围.尽管DB淡定的度过,但是历程中的 ...
- worksteal thread pool
worksteal的场景 对于一个线程池,每个线程有一个队列,想象这种场景,有的线程队列中有大量的比较耗时的任务堆积,而有的线程队列却是空的,现象就是有的线程处于饥饿状态,而有的线程处于消化不良的状态 ...
- Improve Scalability With New Thread Pool APIs
Pooled Threads Improve Scalability With New Thread Pool APIs Robert Saccone Portions of this article ...
- CLR thread pool
Thread Pooling https://msdn.microsoft.com/en-us/library/windows/desktop/ms686756(v=vs.85).aspx Threa ...
- 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 ...
- Thread Pool Engine, and Work-Stealing scheduling algorithm
http://pages.videotron.com/aminer/threadpool.htm http://pages.videotron.com/aminer/zip/threadpool.zi ...
随机推荐
- 1038 Recover the Smallest Number (30分)(贪心)
Given a collection of number segments, you are supposed to recover the smallest number from them. Fo ...
- 【网络安全】——客户端安全(浏览器安全、XSS、CSRF、Clickjacking)
近在学习网络安全相关的知识,于是先从业内一本系统讲Web安全的书<白帽子讲Web安全>系统学习Web安全的相关知识.在此整理书中的知识层次,不求详尽,只求自己对整个Web安全梗概有所了 ...
- app测试的一些较为重要的测试点
安装测试 从不同的手机所自带的不同的版本的软件商城里面下载抖音并安装查看是否成功 安装后是否能正常运行安装后的文件和文件夹是否写到了指定的目录里 安装过程中取消安装,安装的文件是否在指定的目录里 安装 ...
- matplotlib AffineBase, Affine2DBase, Affine2D
2020-04-10 09:02:59 --Edit by yangray 仿射变换矩阵参考资料-->https://blog.csdn.net/robert_chen1988/article/ ...
- AJ学IOS(43)之网易彩票底部自定义TabBar实现切换
AJ分享,必须精品 效果: 代码: NYTabBarController // // NYTabBarController.m // 彩票lottery // // Created by apple ...
- Python 编程环境搭建(Windows 系统中)
由于大家普遍使用 Windows 系统,所以本文只介绍 Windows 系统中 Python 环境的安装. 在 Windows 中安装 Python 与安装普通软件没什么差别,下载所需版本的安装包后, ...
- A. Number Theory Problem
题目大意:计算小于2^n,且满足2^k-1并且是7的倍数的个数 思路:优先打表,数据不大,1e5,然后求个前n项和 #include<bits/stdc++.h> using namesp ...
- Python父类和子类关系/继承
#!/usr/bin/env python # -*- coding: utf-8 -*- """ @File:继承_子类和父类的关系.py @E-mail:364942 ...
- api_DZFPKJ & api_DZFPCX(get_AES_url代码优化)
通过AES加密网站的接口来传值,不需要手动加密字符串后复制过来传值. #coding:utf-8 import requests import re def get_aes_url(key, text ...
- [WEB前置技能]HTTP协议
HTTP概述 HTTP协议是Hyper Text Transfer Protocol(超文本传输协议)的缩写,是用于从万维网(WWW:World Wide Web )服务器传输超文本到本地浏览器的传送 ...