当前位置:网站首页>线程池的使用二
线程池的使用二
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);
}
}
边栏推荐
- 20.nn.Module
- go使用makefile脚本编译应用
- C#控件 ToolStripProgressBar 用法
- 网络层重点协议——IP协议
- C# using NumericUpDown control
- golang-gin-pprof-使用以及安全问题
- P5019 [NOIP2018 提高组] 铺设道路
- 报错:npm ERR code EPERM
- Talk about the message display mechanism on the SAP product UI
- ECCV 2022 | Robotic Interaction Perception and Object Manipulation
猜你喜欢

操作符详解

PyQt5 rapid development and actual combat 9.7 Automated testing of UI layer

365-day challenge LeetCode1000 questions - Day 044 Maximum element in the layer and level traversal

Install the latest pytorch gpu version

VU 非父子组件通信

golang八股文整理(持续搬运)

电脑重要文件很多,如何备份比较安全?

Grab the tail of gold, silver and silver, unlock the programmer interview "Artifact of Brushing Questions"

报错:npm ERR code EPERM

Invalid bound statement (not found)出现的原因和解决方法
随机推荐
聊聊 SAP 产品 UI 上的消息显示机制
PHP序列化:eval
FastAPI encapsulates a generic response
IDEA连接MySQL数据库并执行SQL查询操作
技能大赛dhcp服务训练题
The use of C# control CheckBox
Golang - gin - pprof - use and safety
ASM module in SAP Ecommerce Cloud Spartacus UI and Accelerator UI
为什么 wireguard-go 高尚而 boringtun 孬种
C#控件ListView用法
4.爬虫之Scrapy框架2数据解析&配置参数&数据持久化&提高Scrapy效率
Usage of += in C#
抓住金三银四的尾巴,解锁程序员面试《刷题神器》
Grab the tail of gold, silver and silver, unlock the programmer interview "Artifact of Brushing Questions"
深度剖析 Apache EventMesh 云原生分布式事件驱动架构
查看Oracle数据库的用户名和密码
matlab as(assert dominance)
golang-gin-优雅重启
Even if the image is missing in a large area, it can also be repaired realistically. The new model CM-GAN takes into account the global structure and texture details
Reasons and solutions for Invalid bound statement (not found)