当前位置:网站首页>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);
}
}
边栏推荐
猜你喜欢
ADS communicate with c #
Redis 】 【 publish and subscribe message
技能大赛训练题:登录安全加固
Spark Learning: Add Custom Optimization Rules for Spark Sql
[Blue Bridge Cup Trial Question 46] Scratch Magnet Game Children's Programming Scratch Blue Bridge Cup Trial Question Explanation
C# control StatusStrip use
小试牛刀:Go 反射帮我把 Excel 转成 Struct
多智能体协同控制研究中光学动作捕捉与UWB定位技术比较
How IDEA runs web programs
3.爬虫之Scrapy框架1安装与使用
随机推荐
Shang Silicon Valley-JVM-Memory and Garbage Collection (P1~P203)
C# Get network card information NetworkInterface IPInterfaceProperties
Analysis of the startup source code of hyperf (2) - how the request reaches the controller
C# using ComboBox control
20.nn.Module
IDEA connects to MySQL database and uses data
jOOQ 3.14 released - SQL/XML and SQL/JSON support
csdn发文助手问题
C# control StatusStrip use
八大排序汇总及其稳定性
百度网盘安装在c盘显示系统权限限制的解决方法
使用CompletableFuture进行异步处理业务
Linux bash: redis-server: 未找到命令
Motion capture system for end-positioning control of flexible manipulators
The magic of SQL MERGE statement (detailed instructions)
leetcode:2032. Values that appear in at least two arrays
Spark学习:为Spark Sql添加自定义优化规则
Reasons and solutions for Invalid bound statement (not found)
The operator,
MySQL [aggregate function]