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);
}