当前位置:网站首页>线程池的使用二
线程池的使用二
2022-07-31 13:25:00 【ZeKi_豪】
- 业务场景:
- 导入文件,要处理文件中一堆数据,为了用户体验只能导入后立马返回
上传成功,实际则是直接先记录导入状态(处理中),然后线程池处理完把状态改为处理完成 - 上传文件,将文件存放进自己dfs文件系统,拿到地址url链接,然后生成一个任务id在数据库表中记录及该地址url, 把新建的任务id放入本地缓存
ConcurrentLinkedQueue中
- 上传文件,将文件存放进自己dfs文件系统,拿到地址url链接,然后生成一个任务id在数据库表中记录及该地址url, 把新建的任务id放入本地缓存
- 往线程池中扔线程任务异步处理业务, 异步处理线程任务,任务从本地缓存
ConcurrentLinkedQueue中获取表数据的任务id从而获取该文件的url地址,拿到该文件的数据,然后遍历文件数据进行业务处理,处理完毕则更新数据库状态为处理成功或处理失败
- 往线程池中扔线程任务异步处理业务, 异步处理线程任务,任务从本地缓存
线程池
- 定义线程池,调用方法
execute将线程任务扔进去
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);
// 存放导入文件的任务id
public static ConcurrentLinkedQueue<Integer> threadLinkedQueue = new ConcurrentLinkedQueue<>();
@Override
public void run() {
// 往本地缓存队列拿出数据
Integer taskId = threadLinkedQueue.poll();
//处理后续业务
}
}
实战(参考即可)
- 放入线程任务进线程池
@Autowired
private NumberStateThread numberStateThread;
@Override
public Result uploadFile(MultipartFile file) throws Exception {
//往本地缓存队列放入数据
boolean offerB = NumberStateThread.threadLinkedQueue.offer(mobileTypeState.getId());
if (offerB) {
logger.info("【checkStateNumberUpload】将任务ID[{}]放入本地缓存队列成功", mobileTypeState.getId());
//将线程任务放进线程池执行
BossThreadFactory.execute(numberStateThread);
}
}
边栏推荐
- Introduction to using NPM
- Using SQL Server FOR XML and FOR JSON syntax on other RDBMSs with jOOQ
- Detailed explanation of network protocols and related technologies
- Golang - gin - pprof - use and safety
- 页面整屏滚动效果
- 清除浮动的四种方式及其原理理解
- pytorch gpu版本安装最新
- 技能大赛训练题:MS15_034漏洞验证与安全加固
- 「面经分享」西北大学 | 字节 生活服务 | 一面二面三面 HR 面
- All-round visual monitoring of the Istio microservice governance grid (microservice architecture display, resource monitoring, traffic monitoring, link monitoring)
猜你喜欢
随机推荐
Two methods of NameNode failure handling
C# control ListView usage
golang中使用泛型
Solution for browser hijacking by hao360
SAP 电商云 Spartacus UI 和 Accelerator UI 里的 ASM 模块
Selenium自动化测试之Selenium IDE
IDEA的database使用教程(使用mysql数据库)
图像大面积缺失,也能逼真修复,新模型CM-GAN兼顾全局结构和纹理细节
Batch大小不一定是2的n次幂!ML资深学者最新结论
What should I do if selenium is reversed?
网络协议及相关技术详解
The use of C# control CheckBox
Introduction to using NPM
C#中+=的用法
Adding data nodes and decommissioning data nodes in the cluster
技能大赛训练题:交换机虚拟化练习
SAP message TK 248 solved
IDEA连接MySQL数据库并执行SQL查询操作
MATLAB | 我也做了一套绘图配色可视化模板
技能大赛训练题:ftp 服务攻防与加固









