当前位置:网站首页>The use of thread pool two
The use of thread pool two
2022-07-31 14:08:00 【ZeKi_hao】
- 业务场景:
- 导入文件,To process a bunch of data in the file,For user experience, it can only be imported and returned immediately
上传成功,In fact, the import status is directly recorded first(处理中),Then the thread pool finishes processing and changes the state to处理完成 - 上传文件,Store the file in yourselfdfs文件系统,拿到地址url链接,然后生成一个任务idRecord and this address in the database tableurl, create a new taskid放入本地缓存
ConcurrentLinkedQueue中
- 上传文件,Store the file in yourselfdfs文件系统,拿到地址url链接,然后生成一个任务idRecord and this address in the database tableurl, create a new taskid放入本地缓存
- Throw thread tasks into the thread pool to process business asynchronously, Process thread tasks asynchronously,Tasks are cached locally
ConcurrentLinkedQueuetask to get table dataidto obtain the fileurl地址,Get the data of the file,Then traverse the file data for business processing,After processing, update the database status to处理成功或处理失败
- Throw thread tasks into the thread pool to process business asynchronously, Process thread tasks asynchronously,Tasks are cached locally
线程池
- 定义线程池,调用方法
executeThrow in thread tasks
public class BossThreadFactory {
public static ThreadPoolExecutor threadPoolExecutor;
static {
int corePoolSize = 2;
threadPoolExecutor = new ThreadPoolExecutor(corePoolSize, 2, 1, TimeUnit.SECONDS,
new LinkedBlockingQueue<>(20), new ThreadFactoryBuilder().setNameFormat("boss-%d").build(),
new ThreadPoolExecutor.AbortPolicy());
}
public static void execute(Runnable runnable){
threadPoolExecutor.execute(runnable);
}
}
线程任务
@Component
public class NumberStateThread extends Thread {
private static final Logger logger = LoggerFactory.getLogger(NumberStateThread.class);
// The task that holds the imported fileid
public static ConcurrentLinkedQueue<Integer> threadLinkedQueue = new ConcurrentLinkedQueue<>();
@Override
public void run() {
// Get data from the local cache queue
Integer taskId = threadLinkedQueue.poll();
//处理后续业务
}
}
实战(参考即可)
- Put thread tasks into the thread pool
@Autowired
private NumberStateThread numberStateThread;
@Override
public Result uploadFile(MultipartFile file) throws Exception {
//Put data into the local cache queue
boolean offerB = NumberStateThread.threadLinkedQueue.offer(mobileTypeState.getId());
if (offerB) {
logger.info("【checkStateNumberUpload】将任务ID[{}]Put into the local cache queue successfully", mobileTypeState.getId());
//Put the thread task into the thread pool for execution
BossThreadFactory.execute(numberStateThread);
}
}
边栏推荐
- leetcode:485.最大连续 1 的个数
- Tortoise speed by "template"
- CodeIgniter 打开错误日志
- csdn发文助手问题
- Solution for browser hijacking by hao360
- Linux bash: redis-server: 未找到命令
- “听我说谢谢你”还能用古诗来说?清华搞了个“据意查句”神器,一键搜索你想要的名言警句...
- C# control ToolStripProgressBar usage
- Text similarity calculation (Chinese and English) detailed explanation of actual combat
- Network layer key protocol - IP protocol
猜你喜欢

Comparison of Optical Motion Capture and UWB Positioning Technology in Multi-agent Cooperative Control Research

The use of C# control CheckBox
![MySQL [subquery]](/img/0b/9bbf54c500d85976e6d6776b6c6f13.png)
MySQL [subquery]

使用CompletableFuture进行异步处理业务

Error: npm ERR code EPERM

jvm 一之 类加载器

20.nn.Module

“听我说谢谢你”还能用古诗来说?清华搞了个“据意查句”神器,一键搜索你想要的名言警句...

Batch大小不一定是2的n次幂!ML资深学者最新结论

Resolved (pymysqL connect to the database error) pymysqL. Err. ProgrammingError: (1146, "Table" test. Students' doesn 't exist ")
随机推荐
技能大赛dhcp服务训练题
hyperf的启动源码分析(二)——请求如何到达控制器
百度网盘安装在c盘显示系统权限限制的解决方法
技能大赛训练题:ftp 服务攻防与加固
Resolved (pymysqL connect to the database error) pymysqL. Err. ProgrammingError: (1146, "Table" test. Students' doesn 't exist ")
C# control ListView usage
The batch size does not have to be a power of 2!The latest conclusions of senior ML scholars
C语言基础练(九九乘法表)与打印不同星号图案
numpy矩阵和向量的保存与加载,以及使用保存的向量进行相似度计算
Shell脚本经典案例:文件的备份
MySQL [subquery]
海康摄像机取流RTSP地址规则说明
jOOQ 3.14 released - SQL/XML and SQL/JSON support
LeetCode rotate array
leetcode:2032. 至少在两个数组中出现的值
Numbers that appear only once in LeetCode
ADS communicate with c #
我把问烂了的MySQL面试题总结了一下
BigDecimal 简介,常用方法
DELL SC compellent 康贝存储系统怎么抓取配置信息