当前位置:网站首页>Fork/Join线程池
Fork/Join线程池
2022-08-01 20:22:00 【Q z1997】
- 概念
Fork/Join 是 JDK 1.7 加入的新的线程池实现,它体现的是一种分治思想,适用于能够进行任务拆分的 cpu 密集型运算所谓的任务拆分,是将一个大任务拆分为算法上相同的小任务,直至不能拆分可以直接求解。跟递归相关的一些计算,如归并排序、斐波那契数列、都可以用分治思想进行求解
Fork/Join 在分治的基础上加入了多线程,可以把每个任务的分解和合并交给不同的线程来完成,进一步提升了运算效率
Fork/Join 默认会创建与 cpu 核心数大小相同的线程池 - 使用
提交给 Fork/Join 线程池的任务需要继承 RecursiveTask(有返回值)或 RecursiveAction(没有返回值),例如下面定义了一个对 1~n 之间的整数求和的任务
class AddTask3 extends RecursiveTask<Integer> {
private static final long serialVersionUID = -6423742051855560432L;
int begin;
int end;
public AddTask3(int begin, int end) {
this.begin = begin;
this.end = end;
}
@Override
public String toString() {
return "{" + begin + "," + end + '}';
}
@Override
protected Integer compute() {
// 5, 5
if (begin == end) {
log.debug("join() {}", begin);
return begin;
}
// 4, 5
if (end - begin == 1) {
log.debug("join() {} + {} = {}", begin, end, end + begin);
return end + begin;
}
// 1 5
int mid = (end + begin) / 2; // 3
AddTask3 t1 = new AddTask3(begin, mid); // 1,3
t1.fork();
AddTask3 t2 = new AddTask3(mid + 1, end); // 4,5
t2.fork();
log.debug("fork() {} + {} = ?", t1, t2);
int result = t1.join() + t2.join();
log.debug("join() {} + {} = {}", t1, t2, result);
return result;
}
}
使用
public static void main(String[] args) {
ForkJoinPool pool = new ForkJoinPool(4);
System.out.println(pool.invoke(new AddTask1(5)));
}
边栏推荐
- 【无标题】
- MongoDB快速上手
- Use WeChat official account to send information to designated WeChat users
- LabVIEW 使用VISA Close真的关闭COM口了吗
- PROE/Croe如何编辑已完成的草图,让其再次进入草绘状态
- 【kali-信息收集】(1.4)识别活跃的主机/查看打开的端口:Nmap(网络映射器工具)
- 18. Distributed configuration center nacos
- 实用新型专利和发明专利的区别?秒懂!
- KDD2022 | 自监督超图Transformer推荐系统
- 18、分布式配置中心nacos
猜你喜欢
【torch】张量乘法:matmul,einsum
Get started quickly with MongoDB
第55章 业务逻辑之订单、支付实体定义
Application of Acrel-5010 online monitoring system for key energy consumption unit energy consumption in Hunan Sanli Group
数字孪生北京故宫,元宇宙推进旅游业进程
【Dart】dart构造函数学习记录(含dart单例模式写法)
[Energy Conservation Institute] Application of Intelligent Control Device in High Voltage Switchgear
PROE/Croe如何编辑已完成的草图,让其再次进入草绘状态
【个人作品】无线网络图传模块
nacos installation and configuration
随机推荐
Zheng Xiangling, Chairman of Tide Pharmaceuticals, won the "2022 Outstanding Influential Entrepreneur Award" Tide Pharmaceuticals won the "Corporate Social Responsibility Model Award"
idea插件generateAllSetMethod一键生成set/get方法以及bean对象转换
[Multi-task optimization] DWA, DTP, Gradnorm (CVPR 2019, ECCV 2018, ICML 2018)
我的驾照考试笔记(2)
KDD2022 | 自监督超图Transformer推荐系统
【luogu P1912】诗人小G(二分栈)(决策单调性优化DP)
Remove 360's detection and modification of the default browser
Does LabVIEW really close the COM port using VISA Close?
LTE时域、频域资源
Addition, Subtraction, Multiplication of Large Integers, Multiplication and Division of Large Integers and Ordinary Integers
【Dart】dart构造函数学习记录(含dart单例模式写法)
The graphic details Eureka's caching mechanism/level 3 cache
【节能学院】安科瑞餐饮油烟监测云平台助力大气污染攻坚战
LTE time domain and frequency domain resources
Oracle排序某个字段, 如果这个varchar2类型的字段有数字也有文字 , 怎么按照数字大小排序?
第57章 业务逻辑之业务实体与数据库表的映射规则定义
洛谷 P2440 木材加工
【七夕特别篇】七夕已至,让爱闪耀
内网穿透 lanproxy部署
Redis does web page UV statistics