如下代码:
@Autowired
private 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;
}
@Override
public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
return (throwable, method, objects) -> log.error("Async ERROR: throwable={},method={},params={}", throwable, method, objects);
}
}
使用
@Autowired
@Qualifier("myExecutor") //变量名一致时可无需该注解
private Executor myExecutor;