Java 队列大小超过了spring引导中使用的ThreadPoolTaskExecutor中配置的最大队列容量
请看下面这段代码:Java 队列大小超过了spring引导中使用的ThreadPoolTaskExecutor中配置的最大队列容量,java,spring-boot,threadpool,threadpoolexecutor,Java,Spring Boot,Threadpool,Threadpoolexecutor,请看下面这段代码: @Autowired private ThreadPoolTaskExecutor executor; @PostConstruct public void setupTaskExecutor() { this.executor.setCorePoolSize(getExecutorCorePoolSize()); this.executor.setMaxPoolSize(getExecutorMaxPoolSize()); this.e
@Autowired
private ThreadPoolTaskExecutor executor;
@PostConstruct
public void setupTaskExecutor() {
this.executor.setCorePoolSize(getExecutorCorePoolSize());
this.executor.setMaxPoolSize(getExecutorMaxPoolSize());
this.executor.setQueueCapacity(getExecutorMaxQueueCapacity());
}
public ZoneDetail getZoneDetails(ConfigRequest configRequest) {
LOGGER.debug("QueueSize = {}", executor.getThreadPoolExecutor().getQueue().size());
LOGGER.debug("PoolSize = {}", executor.getPoolSize());
Future<ZoneDetail> future = executor.submit(() -> {
return getPrimaryProvider(context).getZoneDetails(context,
configRequest);
});
try {
ZoneDetail zoneDetail = future.get(getPrimaryProviderTimeout(),
TimeUnit.MILLISECONDS);
} catch (ExecutionException | TimeoutException e) {
// fetch data from secondary provider
}
}
i、 e.我一次打20个线程,持续5秒
在控制台中,我看到队列大小=15,即我的队列大小超过配置的最大队列容量5。和PoolSize=4即,我的池大小从不超过核心池大小,因为额外的线程将进入队列
注意:我遇到了一个调用getZoneDetails()方法的RESTAPI
这是怎么发生的?我做错了什么?- 我认为你的设置不正确。您正在自动连接
,这意味着它已经使用ThreadPoolTaskExecutor
Integer.MAX_值的
初始化了QueueCapacity
,并创建了一个具有该容量的队列。因此,无论您在ThreadPoolExecutor
中执行什么操作,都不会生效setupTaskExecutor()
- 而是一次完成以下操作
- 如果添加
作为LOGGER.debug(“RemainingCapacity={}”,this.executor.getThreadPoolExecutor().getQueue().RemainingCapacity())
的最后一行,它应该会确认这个答案setupTaskExecutor()
- 然后使用它
您确定您正在配置正确的ThreadPoolExecutor,因为您可以拥有很多。@M.Mas。你说的“正确的执行者”是什么意思?我正在使用导入org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor您的任务是使用自动连线ThreadPoolExecutor显式提交的?我不明白是什么方法
getExecutorCorePoolSize
和其他两种方法。它们是您的方法吗?如果是,请写下来。@Itaywazana这些方法是从属性文件读取的字段的getter方法@ConfigurationProperties
在班级级别。谢谢。昨天我也意识到了同样的问题。当我们在创建ThreadPoolTaskExecutor
之后设置queueCapacity
时,它不会传播到ThreadPoolExecutor
方法中初始化的ThreadPoolExecutor
中。感觉这是一个错误。
Threads: 20 Strategy: Simple Test Delay: 0 Limit: 5 seconds
@Bean(name = "myThreadPoolTaskExecutor")
public Executor threadPoolTaskExecutor() {
ThreadPoolTaskExecutor threadPoolTaskExecutor =
new ThreadPoolTaskExecutor();
threadPoolTaskExecutor.setCorePoolSize(getExecutorCorePoolSize());
threadPoolTaskExecutor.setMaxPoolSize(getExecutorMaxPoolSize());
threadPoolTaskExecutor.setQueueCapacity(getExecutorMaxQueueCapacity());
threadPoolTaskExecutor.initialize();
return threadPoolTaskExecutor;
}
@Autowired
@Qualifier("myThreadPoolTaskExecutor")
public ThreadPoolTaskExecutor executor;