Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/379.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 队列大小超过了spring引导中使用的ThreadPoolTaskExecutor中配置的最大队列容量_Java_Spring Boot_Threadpool_Threadpoolexecutor - Fatal编程技术网

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;