Reputation: 1468
Why Spring's ThreadPoolTaskExecutor keep on creating threads up to Core Size value, no matter existing threads are idle!
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(300);
executor.setMaxPoolSize(500);
executor.setQueueCapacity(5000);
executor.setThreadNamePrefix("AsyncTask-");
executor.initialize();
I fired requests one by one and it kept on increasing the number of thread until it reached 300. My question is if an existing thread is idle, why it isn't using the idle one? Once the Core Pool size is reached its anyway using the threads from pool only.
Upvotes: 3
Views: 2637
Reputation: 542
Core thread pool size specifies the number of threads that are to be kept ready to handle any potential work to avoid the overhead of creating new threads. Specifying 300 is asking for a threadpool that maintains 300 threads at all time, you shouldn't expect it to reuse before the number is met. If this is too high, consider decreasing the corePoolSize while maintaining the same maxPoolSize, it scales up when the pool is overloaded. Check out ThreadPoolTaskExecutor's setAllowCoreThreadTimeOut to scale down the thread pool below corePoolSize when not in use.
Upvotes: 2
Reputation: 2340
Spring has less to do here as per java documentation 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
Upvotes: 4