Spring boot 线程池之单线程问题

 2023-09-05 阅读 41 评论 0

摘要:如下代码: @Autowiredprivate Executor taskScheduler;...CompletionService<List<DmpStockResponseDTO>> completionService = new ExecutorCompletionService<>(taskScheduler);List<List<Triple<Date, ProvinceCityTagDTO, Stri

如下代码: 

		@Autowiredprivate Executor taskScheduler;...CompletionService<List<DmpStockResponseDTO>> completionService = new ExecutorCompletionService<>(taskScheduler);List<List<Triple<Date, ProvinceCityTagDTO, String>>> partition = Lists.partition(sqlList, 20);List<Future<List<DmpStockResponseDTO>>> futureList = new ArrayList<>(partition.size() * 2);for (List<Triple<Date, ProvinceCityTagDTO, String>> triples : partition) {futureList.add(completionService.submit(() -> allPv(req, triples)));}try {for (Future<List<DmpStockResponseDTO>> future : futureList) {list.addAll(future.get());}} catch (InterruptedException | ExecutionException e) {log.error(e.getMessage(), e);}

运行时打印日志,发现是单线程在跑😅

安排:

方案一是添加如下配置:

# 一、任务调度线程池:
# taskScheduler=org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler
# 任务调度线程任务是串行执行,在使用@Scheduled注解时根据定时任务动态调整该参数
# 默认是1
spring.task.scheduling.pool.size=8
spring.task.scheduling.thread-name-prefix=my-scheduling# 二、任务执行线程池配置
# 以下配置只有在开启@EnableAsync且@Async同时存在时生效且自动新增一个名为:
# applicationTaskExecutor=org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor线程池
spring.task.execution.pool.core-size=8
spring.task.execution.pool.max-size=17
spring.task.execution.pool.max-size=200
...

方案二是自定义,如下:

import lombok.extern.slf4j.Slf4j;
import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.AsyncConfigurer;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;/*** 异步线程池配置** @author songjianyong*/
@Configuration
@Slf4j
public class AsyncConfig implements AsyncConfigurer {@Override@Bean(name = "myExecutor")public Executor getAsyncExecutor() {ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();executor.setCorePoolSize(Runtime.getRuntime().availableProcessors());executor.setMaxPoolSize(executor.getCorePoolSize() * 2 + 1);executor.setQueueCapacity(500);executor.setKeepAliveSeconds(60);executor.setThreadNamePrefix("my-executor");executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());executor.initialize();return executor;}@Overridepublic AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {return (throwable, method, objects) -> log.error("Async ERROR: throwable={},method={},params={}", throwable, method, objects);}
}

使用

    @Autowired@Qualifier("myExecutor") //变量名一致时可无需该注解private Executor myExecutor;

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://808629.com/1174.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 86后生记录生活 Inc. 保留所有权利。

底部版权信息