gpt4 book ai didi

java - 队列大小超过了在 spring boot 中使用的 ThreadPoolTask​​Executor 中配置的最大队列容量

转载 作者:行者123 更新时间:2023-12-05 03:51:31 25 4
gpt4 key购买 nike

请看下面这段代码:

@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
}

}

我配置的值是
核心池大小=4
最大池大小=16
最大队列容量=5

我使用带有以下参数的 SoapUI 运行 PT,

Threads: 20  Strategy: Simple Test Delay: 0 Limit: 5 seconds

即我一次使用 20 个线程,持续 5 秒。

在控制台中,我看到 QueueSize = 15 即我的队列大小超过配置的最大队列容量 5。PoolSize = 4 即我的池大小从未超过核心池大小,因为额外的线程正在进入队列。

注意:我正在访问调用 getZoneDetails() 方法的 REST API

这是怎么发生的?我做错了什么?

最佳答案

  • 我认为您的设置不正确。您正在 Autowiring ThreadPoolTaskExecutor这意味着它已经初始化了 ThreadPoolExecutorQueueCapacityInteger.MAX_VALUE并创建了一个具有该容量的队列。所以无论你在 setupTaskExecutor() 做什么不会生效。

  • 改为一次完成以下操作

    @Bean(name = "myThreadPoolTaskExecutor")
public Executor threadPoolTaskExecutor() {
ThreadPoolTaskExecutor threadPoolTaskExecutor =
new ThreadPoolTaskExecutor();
threadPoolTaskExecutor.setCorePoolSize(getExecutorCorePoolSize());
threadPoolTaskExecutor.setMaxPoolSize(getExecutorMaxPoolSize());
threadPoolTaskExecutor.setQueueCapacity(getExecutorMaxQueueCapacity());
threadPoolTaskExecutor.initialize();
return threadPoolTaskExecutor;
}
  • 如果添加 LOGGER.debug("RemainingCapacity = {}", this.executor.getThreadPoolExecutor().getQueue().remainingCapacity())作为 setupTaskExecutor() 的最后一行, 它应该确认这个答案

  • 然后使用它

    @Autowired
@Qualifier("myThreadPoolTaskExecutor")
public ThreadPoolTaskExecutor executor;

关于java - 队列大小超过了在 spring boot 中使用的 ThreadPoolTask​​Executor 中配置的最大队列容量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62833494/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com