当前位置:网站首页>Use of forkjoin
Use of forkjoin
2022-07-22 00:45:00 【Angry Apple ext】
Catalog
Preface
- ForkJoin yes JDK1.7 Multi thread parallel processing framework added .ForkJoin Use
Divide and rule
Thought , Divide a big task into small tasks , And then aggregate , Get the final result . It's a bit like Hadoop Medium MapReduce. It also supports work theft . - Attached below ForkJoin Java Concurrent animation .
This Jar Package download address : https://sourceforge.net/projects/javaconcurrenta/files/, There are also many interesting animations , Help us learn JUC.
What is job theft : Suppose there is A、B Two threads execute a task ,A Faster , Finish the work , Now A You can put B Part of the live connection . In general, this will speed up the task execution .
application
demand
- Suppose there is such a need : I want to count all the amounts in the user table . This watch has
17000003
Data . If I use SQL Statistics are slow , As shown in the figure below . - It cost 4.563 Seconds to find out .
- I found that every time
1000000
It's still very fast , As shown in the figure below . - I just wonder if I can write a program , Split into multiple small tasks , Batch query , Then merge the results .
Use
according to id Range query SUM
... Omit ...
@Override
public long sumRecord(int toId, int fromId) {
QueryWrapper<Users> queryWrapper = new QueryWrapper<>();
// use in The statements are combined into one SQL, Avoid repeatedly requesting the database IO
queryWrapper.ge("id", fromId);
queryWrapper.le("id", toId);
queryWrapper.select("IFNULL(SUM(money),0) as money");
List<Users> users = usersMapper.selectList(queryWrapper);
if (!CollectionUtils.isEmpty(users)) {
return users.get(0).getMoney();
}
return 0;
}
... Omit ...
Create task classes and test cases
... Omit ...
@Test
public void sumTask() {
long startTime = System.currentTimeMillis();
ForkJoinPool pool = new ForkJoinPool(Runtime.getRuntime().availableProcessors() * 2);
// Simulate tens of millions of data
int min = 1;
int max = 17000003;
SumTask sumTask = new SumTask(min, max, userService);
pool.invoke(sumTask);
System.out.println(" total " + sumTask.join() +
" execution time " + (System.currentTimeMillis() - startTime));
}
public static final Integer THRESHOLD = 1000000;
public static class SumTask extends RecursiveTask<Long> {
int fromId;
int toId;
private UserService userService;
public SumTask(int fromId, int toId, UserService userService) {
this.fromId = fromId;
this.toId = toId;
this.userService = userService;
}
@Override
protected Long compute() {
if (toId - fromId < THRESHOLD) {
return sumRecord(toId, fromId);
} else {
int mid = (fromId + toId) / 2;
SumTask left = new SumTask(fromId, mid, userService);
SumTask right = new SumTask(mid + 1, toId, userService);
invokeAll(left, right);
return left.join() + right.join();
}
}
public Long sumRecord(int toId, int fromId) {
System.out.println(" Parameters " + fromId + " " + toId);
return userService.sumRecord(toId, fromId);
}
}
... Omit ...
Execution results
- The execution speed is obviously faster .
Summary
- We can do it in new ForkJoinPool(int parallelism) Number of incoming threads ( The default is CPU The core number ), tuning .
- If it's inheritance RecursiveAction: For tasks that do not return results .
Complete code
边栏推荐
- 原来它在数据中台建设中这么重要!
- 统计一年中每个月的数据
- uniapp使用图表
- Several usages of return statement in go language
- Special writing method of if statement in go language (if with semicolon;)
- 场景化数据应用,企业业务创新的内在驱动力
- 【自然语言处理与文本分析】文本特征提取方法总结。关键词提取方法。公认效果较好的IDF,RCF。
- How to join enterprise wechat group chat with applets
- 云原生与低代码平台成就敏捷企业
- 基于无法安装64位版本的visio,因为在您的PC上找到了以下32位程序的解决办法
猜你喜欢
RFC document download
【特征学习】以深度学习和词嵌入为基础的特征学习
【自然语言处理和文本分析】基础信息检索:签名文件技术,进阶信息检索:向量空间技术(目前主流的搜索引擎在用的技术)
[natural language processing and text analysis] basic information retrieval: signature file technology, advanced information retrieval: vector space technology (technology currently used by mainstream
[natural language processing and text analysis] summary of text feature extraction methods. Keyword extraction method. IDF and RCF with good effect are recognized.
云原生与低代码平台成就敏捷企业
[grafana] install MySQL under CentOS + grafana add MySQL data source
小程序如何使用自定義導航欄
一个程序员的水平能差到什么程度?
Download of typora and use of markdown
随机推荐
VLAN advanced technology
mysql统计表的列数
Oracle设置最大连接数
批量转移(复制)文件夹内同类型文件(win10)
【进阶数据挖掘技术】进阶数据挖掘技术介绍
云原生与低代码平台成就敏捷企业
【 traitement du langage naturel et analyse de texte】 cet article présente la méthodologie de l'exploration de texte avec deux cas de projet.
[natural language processing and text analysis] PCA text dimensionality reduction. Singular value decomposition SVD, Pu decomposition method. Unsupervised word embedding model glove. The method of tra
[natural language processing and text analysis] basic information retrieval: signature file technology, advanced information retrieval: vector space technology (technology currently used by mainstream
如何导出异步的数据两种方法
【自然语言处理与文本分析】非结构文本转结构数据。BP神经网络,反向传播神经网络,神经网络优化的底层原理,梯度优化法
Native applets use longitude and latitude to resolve addresses
【特征选择】特征选择的几种方法
dataframe 统计重复次数
[100 unity practical skills] | list in C The find method looks for the first element that meets the requirements and returns
Uniapp usage chart
在vscode里配置rust时遇到error:linking with ‘x86_64-w64-mingw32-gcc‘failed:exit code:1
小程序如何实现 商品的sku 筛选
Several methods of quickly checking the local public IP
[natural language processing and text analysis] overview of natural language processing