当前位置:网站首页>多线程应用
多线程应用
2022-07-16 20:46:00 【枫林残@】
一、定义线程池
@Configuration
@EnableAsync
public class ThreadPoolConfig implements AsyncConfigurer {
@Bean(Constant.THREAD_BEAN_NAME)
public ThreadPoolTaskExecutor taskExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(Constant.THREAD_CORE_POOL_SIZE);
executor.setMaxPoolSize(Constant.THREAD_MAX_POOL_SIZE);
executor.setQueueCapacity(Constant.THREAD_QUEUE_CAPACITY);
executor.setKeepAliveSeconds(Constant.THREAD_KEEP_ALIVE_TIME);
executor.setThreadNamePrefix(Constant.THREAD_NAME_PREFIX);
// 线程池对拒绝任务的处理策略
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
// 初始化
executor.initialize();
return executor;
}
}二、引用线程池
public class ExecuteTaskServiceImpl implements ExecuteTaskService {
@Autowired
@Qualifier(value = Constant.THREAD_BEAN_NAME)
private ThreadPoolTaskExecutor poolTaskExecutor;
@Override
public ResponseEntity<ResultInfo<RunTimeProcessCountStatisticVO>> runProcessCountStatistics(ProcessCountStatisticDTO processCountStatisticDTO) {
AtomicInteger executionCompleteCount = new AtomicInteger();
AtomicInteger executionInProcessCount = new AtomicInteger();
try {
List<HistoricProcessInstance> list = Collections.synchronizedList(historicProcessInstanceList);
CountDownLatch latch = new CountDownLatch(list.size());
list.forEach(item -> poolTaskExecutor.execute(() -> multiHandleRunTimeProcess(item, historicTaskInstancesList, executionCompleteCount, executionInProcessCount, endDate, latch)));
latch.await();
} catch (Exception e) {
log.error(e.getMessage());
}
}
private void multiHandleRunTimeProcess(HistoricProcessInstance historicProcessInstance, Set<String> historicTaskInstancesList,AtomicInteger executionCompleteCount, AtomicInteger executionInProcessCount, Date endDate, CountDownLatch latch) {
if (historicProcessInstance.getEndTime() != null && historicProcessInstance.getEndTime().before(endDate) && historicTaskInstancesList.contains(historicProcessInstance.getId())) {
executionCompleteCount.getAndIncrement();
} else if (historicProcessInstance.getEndTime() == null && historicTaskInstancesList.contains(historicProcessInstance.getId())) {
executionInProcessCount.getAndIncrement();
}
latch.countDown();
}
}注意:当统计数量时要用保证数字的原子性,使用AtomicInteger,也可使用同步锁(synchronized)将逻辑锁住
/**
* 任务数量统计多线程
*/
private void countDataMulHandle(TaskListDTO item, TaskCountStatisticVO vo, CountDownLatch latch) {
int count = taskCountHandle(item).size();
synchronized (this) {
if (item.getTaskFilter().equals(Constant.STR_ALL)) {
vo.setAll(count);
} else if (item.getTaskFilter().equals(Constant.STR_SELF)) {
vo.setSelf(count);
} else {
vo.setNotice(count);
}
}
latch.countDown();
}边栏推荐
- Leetcode -- intersection of two arrays 2
- Solution to Chinese garbled code in response results of burpsuite tool
- Web development from entry to proficiency I (detailed)
- BurpSuite工具响应结果中文乱码解决方法
- Tencent is all around. It's silly to ask
- Marvell88Q5192 switch调试记录(BSTA1000B平台)
- pytorch的安装(非常详细)
- What if the work of product evaluators is repetitive and cumbersome? Can it be automated?
- JS binary search pseudocode
- 20220714给AIO-3568J适配OpenHarmony-v3.1-beta(编译Buildroot)
猜你喜欢

Why is there an error during ucosiiv2.93 migration? The error message is undefined symbol '_ OSTaskReturnHook‘

Program analysis and Optimization - 11 multi branch analysis

Sklearn linear regression completes the fitting of multiple term function and sine function

416. Partition equal sum subset · knapsack problem · dynamic programming

Peking University and Microsoft jointly proposed a super time series representation learning framework, which significantly improved the effect of multiple time series tasks

BurpSuite工具响应结果中文乱码解决方法

leetcode--两个数组交集2

Problem solving -- > online OJ (16)

20220714给AIO-3568J适配OpenHarmony-v3.1-beta(编译Buildroot)

三维点云课程(一)——点云基础介绍
随机推荐
练习动画最好的方式:封面过渡
Marvell88q5192 switch debugging record (bsta1000b platform)
SIEMENS模块6DD1661-0AE1
Sklearn linear regression fitting first-order term function
sklearn线性回归完成多次项函数和正弦函数拟合
UCOS III learning notes (11) -- task semaphores and task message queues
Common problems of database
Encapsulate and obtain system user information, roles and authority control
产品测评师工作重复繁琐怎么办?能实现自动化吗?
(post game supplement) (great DFS) k - counting time
Myql database suddenly disappeared, how to restore it?
Why is there an error during ucosiiv2.93 migration? The error message is undefined symbol '_ OSTaskReturnHook‘
leetcode--49字母异位词分组
The best way to practice Animation: cover transition
2 - Conan Binary package Dependence Management Scheme
C language custom types: structure, enumeration, union
C # - adding thread, loading case of progress bar, adding video effect for the first time
Marvell88Q5192 switch调试记录(BSTA1000B平台)
时序事件数据怎么处理?【慕尼黑工业大学博士论文】神经时序点过程(NTPP): 连续时间事件数据建模
Shallow copy and deep copy