栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Spring boot 线程池之单线程问题

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Spring boot 线程池之单线程问题

如下代码: 

		@Autowired
        private Executor taskScheduler;

...
		
        CompletionService> completionService = new ExecutorCompletionService<>(taskScheduler);
        List>> partition = Lists.partition(sqlList, 20);
        List>> futureList = new ArrayList<>(partition.size() * 2);
        for (List> triples : partition) {
            futureList.add(completionService.submit(() -> allPv(req, triples)));
        }


        try {
            for (Future> 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;


@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;
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/777263.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号