当前位置:网站首页>Multithreaded quick processing list collection (combined with the use of thread pool)
Multithreaded quick processing list collection (combined with the use of thread pool)
2022-07-20 07:01:00 【Boring people in the morning】
There is a big one List aggregate , Traversal does some time-consuming operations , Unable to meet the performance requirements , Query log , Although there are many databases and third parties for a single task API request , More time-consuming , But the return efficiency is acceptable , Therefore, multithreading is preferred to process parallel requests, databases and third parties API, Because you have to deal with list Operate on the data to which it belongs , therefore , Thread pool multithreading processing needs to wait for all processing .
Code up :
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class testList {
public static void main(String [] args) {
List<String> list = new ArrayList<>();
for (int i = 1; i <= 900; i++) {
list.add("a"+i);
}
ExecutorService touchWorker = Executors.newFixedThreadPool(9, Executors.defaultThreadFactory());
int size = list.size();
long start=System.currentTimeMillis();
if (size > 100) {
int batch = size % 100 == 0 ? size / 100 : size / 100 + 1; // take List Set slicing
for (int j = 0; j < batch; j++) {
int end = (j + 1) * 100;
if (end > size) {
end = size;
}
List<String> subList = list.subList(j * 100, end);// Intercept the data of each small fragment The first is 0-100 And so on
touchWorker.execute(()-> sleepTest(subList));// Let the thread pool perform work
}
touchWorker.shutdown();// Close thread pool
while (true) {
if (touchWorker.isTerminated()) {
break;
}
}
} else {
sleepTest(list);
}
long end =System.currentTimeMillis();
System.out.println((end-start)/1000);
}
private static void sleepTest(List<String> subList) {
for (String i : subList) {
try {
// The long-running
System.out.println("######" + i + "######" + Thread.currentThread().getName());
Thread.sleep(100);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
Execution results :
A total of time consuming 10s; As you can see above, let each operation sleep 0.1s, And then it started 9 Threads , The pieces are also divided 9 A movie , So every thread gets 100 Data processing , This is the fastest way , If the fixed thread pool is on 5 A thread , The same operation , Look at the current execution time :
when 20s, You can also think of , front 5 One processing 500 It's used 10 second , Back 4 Threads handle the rest 400 It also takes time 10 second . Therefore, in the actual work, the number of threads in the thread pool can be dynamically adjusted according to the size of the fragment .
void shutdown()
Start a sequence close , Perform previously submitted tasks , But don't take on new tasks . If closed , The call has no other effect .
Throw out :SecurityException - If the security manager exists and closes , this ExecutorService It is possible to operate on some threads that do not allow the caller to modify ( Because it doesn't keep RuntimePermission(“modifyThread”)), Or the security manager checkAccess Method denied access .
boolean isTerminated()
If all tasks have been completed after closing , Then return to true. Note that unless you first call shutdown or shutdownNow, otherwise isTerminated Never for true. return : If all tasks have been completed after closing , Then return to true.
As for which thread pool to use, you can see the following
Refer to the article
Reference article :
https://blog.csdn.net/qililong88/article/details/114320641
Introduction and use examples of four common thread pools
Use an infinite thread pool newCachedThreadPool Possible problems
Sort and collect several multi-threaded segmentation processing list The way to assemble
边栏推荐
- Silicon Valley class lesson 10 - marketing module and official account menu management
- [HAOI2012] 音量调节(可达背包)
- G2. Passable Paths (hard version) (树的直径 + lca)
- LeetCode Algorithm 147. 对链表进行插入排序
- MySQL中的慢查询日志
- 这几款实用的安全浏览器插件,让你效率提高
- 创未来 享非凡 | GBASE南大通用亮相openGauss Developer Day 2022
- 股票账户上买基金安全吗,专业解答
- 安装了SSL证书,网站为什么还会出现不安全提示?
- 电赛猜题?我觉得没用,还不如做好这些!
猜你喜欢
随机推荐
CD from grabbing the track to building a streaming media server -- a case study of "moon in the hometown of sleep"
U++常用类型转化及Lambda的常用形式及代理
Complete solution of tsconfig common configuration
今日直播|Apache Pulsar Meetup:vivo、腾讯云、BIGO、云兴科技实践分享
Redis deletion strategy and elimination strategy
MySQL中的慢查询日志
微服务2-nacos注册中心
shell练习题
Complete solution of tsconfig common configuration
回归分析模型
make和Makefile总结一
[literature reading] npe: an FPGA based overlay processor for natural language
Detailed WC find xargs zip gzip bzip2 XZ tar SFTP command or protocol
ES6中set、map、DOM classList的基础用法
这几款实用的安全浏览器插件,让你效率提高
sql 请问如何在输入查询条件为空的情况下返回所有的数据
pdf.js 使用介绍
UXDB如何在多个处理器上工作
NFT访问工具PREMINT遭黑 损失超37万美元
[leetcode daily question] - 108 Convert an ordered array into a binary search tree