Springboot ThreadPoolTaskExecutor使用

application.properties,设置线程池参数

log-service.thread-pool.receiver-log.core-pool-size=12
log-service.thread-pool.receiver-log.max-pool-size=40
log-service.thread-pool.receiver-log.queue-capacity=100
log-service.thread-pool.receiver-log.keep-alive-seconds=100

参数解释 

log-service.thread-pool.receiver-log.core-pool-size=12  核心线程数
log-service.thread-pool.receiver-log.max-pool-size=40  最大线程数
log-service.thread-pool.receiver-log.queue-capacity=100  线程最大空闲时间
log-service.thread-pool.receiver-log.keep-alive-seconds=100  队列大小

 线程池配置

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

/**
 * 线程池配置。
 * @author Gotham
 * Create Date: 2019-09-09
 */
@Configuration
public class ThreadPoolConfig {

    @Bean("ReceiverLogThreadPoolTaskExecutor")
    public ThreadPoolTaskExecutor receiverLogThreadPoolTaskExecutor(
            @Value("${log-service.thread-pool.receiver-log.core-pool-size}") int corePoolSize,
            @Value("${log-service.thread-pool.receiver-log.max-pool-size}") int maxPoolSize,
            @Value("${log-service.thread-pool.receiver-log.queue-capacity}") int queueCapacity,
            @Value("${log-service.thread-pool.receiver-log.keep-alive-seconds}") int keepAliveSeconds) {
        ThreadPoolTaskExecutor threadPoolTaskExecutor = setThreadPoolTackExecutor(corePoolSize, maxPoolSize,
                queueCapacity, keepAliveSeconds);
        threadPoolTaskExecutor.setThreadNamePrefix("receiverLogThreadExecutor-");

        return threadPoolTaskExecutor;
    }

    @Bean("PullLogThreadPoolTaskExecutor")
    public ThreadPoolTaskExecutor pullLogThreadPoolTaskExecutor(
            @Value("${log-service.thread-pool.pull-log.core-pool-size}") int corePoolSize,
            @Value("${log-service.thread-pool.pull-log.max-pool-size}") int maxPoolSize,
            @Value("${log-service.thread-pool.pull-log.queue-capacity}") int queueCapacity,
            @Value("${log-service.thread-pool.pull-log.keep-alive-seconds}") int keepAliveSeconds) {

        ThreadPoolTaskExecutor threadPoolTaskExecutor = setThreadPoolTackExecutor(corePoolSize, maxPoolSize,
                queueCapacity, keepAliveSeconds);
        threadPoolTaskExecutor.setThreadNamePrefix("pullLogThreadExecutor-");

        return threadPoolTaskExecutor;
    }

    private ThreadPoolTaskExecutor setThreadPoolTackExecutor ( int corePoolSize, int maxPoolSize, int queueCapacity, int keepAliveSeconds){
        var threadPoolTaskExecutor = new ThreadPoolTaskExecutor();
        threadPoolTaskExecutor.setCorePoolSize(corePoolSize);
        threadPoolTaskExecutor.setMaxPoolSize(maxPoolSize);
        threadPoolTaskExecutor.setQueueCapacity(queueCapacity);
        threadPoolTaskExecutor.setKeepAliveSeconds(keepAliveSeconds);

        return threadPoolTaskExecutor;
    }

}

使用

@Component
public class Demo{
    private ThreadPoolTaskExecutor threadPoolTaskExecutor;
    @Autowired
    public LogAgentReceiver(
            @Qualifier("ReceiverLogThreadPoolTaskExecutor") ThreadPoolTaskExecutor threadPoolTaskExecutor){
        this.threadPoolTaskExecutor = threadPoolTaskExecutor;
    }
}

@PostConstruct 
    private void init() {
        future = threadPoolTaskExecutor.submit(this::listen);
    }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值