Spring boot 线程池之单线程问题

如下代码: 

		@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;
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值