gpt4 book ai didi

java - 线程池任务执行器

转载 作者:行者123 更新时间:2023-11-30 07:40:11 25 4
gpt4 key购买 nike

为什么 Spring 的 ThreadPoolTask​​Executor 会继续创建达到 Core Size 值的线程,无论现有线程是否空闲!

    ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(300);
executor.setMaxPoolSize(500);
executor.setQueueCapacity(5000);
executor.setThreadNamePrefix("AsyncTask-");
executor.initialize();

我一个一个地发出请求,它不断增加线程数,直到达到 300。我的问题是,如果现有线程空闲,为什么它不使用空闲线程?一旦达到核心池大小,无论如何都只能使用池中的线程。

enter image description here

最佳答案

根据 java 文档,Spring 在这方面的工作较少 here

When a new task is submitted in method execute(Runnable), and fewer than corePoolSize threads are running, a new thread is created to handle the request, even if other worker threads are idle. If there are more than corePoolSize but less than maximumPoolSize threads running , a new thread will be created only if the queue is full

关于java - 线程池任务执行器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58061227/

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