What is the difference between corePoolSize and maxPoolSize in the Spring ThreadPoolTaskExecutor
Here are Sun’s rules for thread creation in simple terms:
- If the number of threads is less than the
corePoolSize
, create a new Thread to run a new task. - If the number of threads is equal (or greater than) the
corePoolSize
, put the task into the queue. - If the queue is full, and the number of threads is less than the
maxPoolSize
, create a new thread to run tasks in. - If the queue is full, and the number of threads is greater than or equal to
maxPoolSize
, reject the task.
Full article
Origin answer
The javadoc says it best:
When a new task is submitted [...], 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 thancorePoolSize
but less thanmaximumPoolSize
threads running, a new thread will be created only if the queue is full. By settingcorePoolSize
andmaximumPoolSize
the same, you create a fixed-size thread pool. By settingmaximumPoolSize
to an essentially unbounded value such asInteger.MAX_VALUE
, you allow the pool to accommodate an arbitrary number of concurrent tasks.
As for your specific situation, sending 500 emails all at the same time is pointless, you'll just overwhelm the mail server. If you need to send a large number of emails, then use a single thread, and send them down the pipe one at a time. The mail server will handle this much more gracefully than 500 separate connections.
corePoolSize
is the minimum number of threads used by the pool. The number can increase up to maxPoolSize
. When the load goes down, the pool will shrink back to corePoolSize
.
Sending email seems to be an I/O bound operation. I do not think having 500 threads will make it faster.