当前位置:网站首页>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
ConcurrentLinkedQueue
task 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
线程池
- 定义线程池,调用方法
execute
Throw 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);
}
}
边栏推荐
猜你喜欢
Reasons and solutions for Invalid bound statement (not found)
MySQL【聚合函数】
Motion capture system for end-positioning control of flexible manipulators
Open Inventor 10.12 Major Improvements - Harmony Edition
已解决(pymysqL连接数据库报错)pymysqL.err.ProgrammingError: (1146,“Table ‘test.students‘ doesn‘t exist“)
C#高级--委托
endnote引用
jvm 一之 类加载器
Resnet&API
爱可可AI前沿推介(7.31)
随机推荐
Analysis of the startup source code of hyperf (2) - how the request reaches the controller
1-hour live broadcast recruitment order: industry leaders share dry goods, and enterprise registration is open丨qubit · point of view
IDEA connects to MySQL database and uses data
VU 非父子组件通信
Sliding window method to segment data
Error: npm ERR code EPERM
For enterprises in the digital age, data governance is difficult, but it should be done
The recently popular domestic interface artifact Apipost experience
Open Inventor 10.12 Major Improvements - Harmony Edition
“听我说谢谢你”还能用古诗来说?清华搞了个“据意查句”神器,一键搜索你想要的名言警句...
纸质说明书秒变3D动画,斯坦福大学吴佳俊最新研究,入选ECCV 2022
go使用makefile脚本编译应用
Shell script classic case: backup of files
图像大面积缺失,也能逼真修复,新模型CM-GAN兼顾全局结构和纹理细节
多智能体协同控制研究中光学动作捕捉与UWB定位技术比较
Node version switching management using NVM
技能大赛训练题:交换机虚拟化练习
C# List Usage List Introduction
Shell脚本经典案例:探测批量主机是否存活
The magic of SQL MERGE statement (detailed instructions)