当前位置:网站首页>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 的个数
- Unity study notes Description of AVPro video jump function (Seeking)
- 五个维度着手MySQL的优化
- C# List Usage List Introduction
- 组合系列--有排列就有组合
- 图像大面积缺失,也能逼真修复,新模型CM-GAN兼顾全局结构和纹理细节
- 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
- 已解决(pymysqL连接数据库报错)pymysqL.err.ProgrammingError: (1146,“Table ‘test.students‘ doesn‘t exist“)
- C# control StatusStrip use
- Open Inventor 10.12 重大改进--和谐版
猜你喜欢

Linux bash: redis-server: command not found

使用NVM进行node版本切换管理

Samba 远程命令执行漏洞(CVE-2017-7494)

MySQL has played to such a degree, no wonder the big manufacturers are rushing to ask for it!

232层3D闪存芯片来了:单片容量2TB,传输速度提高50%

AWS实现定时任务-Lambda+EventBridge

C#高级--委托

技能大赛dhcp服务训练题

「面经分享」西北大学 | 字节 生活服务 | 一面二面三面 HR 面

海康摄像机取流RTSP地址规则说明
随机推荐
[Blue Bridge Cup Trial Question 46] Scratch Magnet Game Children's Programming Scratch Blue Bridge Cup Trial Question Explanation
MySQL【聚合函数】
Why do we need to sub-library and sub-table?
Nuget打包并上传教程
leetcode:485.最大连续 1 的个数
技能大赛训练题:域用户和组织单元的创建
IDEA连接MySQL数据库并使用数据
Solution for browser hijacking by hao360
el-tooltip的使用
[Niu Ke brush questions - SQL big factory interview questions] NO3. E-commerce scene (some east mall)
IDEA connects to MySQL database and uses data
Linux bash: redis-server: 未找到命令
技能大赛训练题:登录安全加固
C# control StatusStrip use
对数字化时代的企业来说,数据治理难做,但应该去做
C#高级--委托
【Pytorch】F.softmax()方法说明
Uniapp WeChat small application reference standard components
AI cocoa AI frontier introduction (7.31)
百度网盘安装在c盘显示系统权限限制的解决方法