What is the difference between corePoolSize and maxPoolSize in the Spring ThreadPoolTaskExecutor

Viewed 46631

I have to send out massEmails to all users of a website. I want to use a thread pool for each email that is sent out. Currently I have set the values to :

<property name="corePoolSize" value="500" />
<property name="maxPoolSize" value="1000" />

What is the difference between the two and will it scale. Currently I have approx. 10000 users.

7 Answers

We already have so many answers and would suffice, but I always confused every other day. So I came up with a real time example to relate this!

A simple and efficient way to get this is by considering yourself being in a bank.

You ever stood in a line even though there is a window available? Why would the bank keep a customer in a queue if there are available cash-window? That's exactly the case with core pool size. If there are un-used threads, new tasks are directly allocated to them.

enter image description here

One the available window(core pool size) is full, customers are asked to wait in the queue. This is where the queue-capacity comes in picture! New tasks are keep on queueing until no more tasks can be queued.

enter image description here

What if the wait-lounge is full of customers? Bank can allocate/open new cash-windows as they see spike in number of customers(tasks). They had 3 more in reserve. Here comes the max pool size in context.

Once these windows(threads) are all occupied, Bank can no longer server any new customers(tasks). Agree? And henceforth, new tasks are not accepted!

enter image description here

In addition to what @skaffman pointed out from official docs, following also makes it more clear how these sizes are utilized:

Any BlockingQueue may be used to transfer and hold submitted tasks. The use of this queue interacts with pool sizing:

  • If fewer than corePoolSize threads are running, the Executor always prefers adding a new thread rather than queuing.
  • If corePoolSize or more threads are running, the Executor always prefers queuing a request rather than adding a new thread.
  • If a request cannot be queued, a new thread is created unless this would exceed maximumPoolSize, in which case, the task will be rejected.

What everyone has explained so clearly is correct. Few things to notice here is that you should always have a limited size for core-pool as well as queue. If the core-pool-size if very high, there can a high chance that many of your threads from pool are remaining unused for certain period of time since for every request new thread gets created until it reaches max-pool-size

But if your machine is going to face large number of request concurrently then you should also consider that the machine size is sufficient enough : example:

If your machine size in 1 GB and queue capacity is at Integer.MAX_VALUE then there is a high chance that your machine will start rejecting the requesting at some point of time because of OutOfMemory which you can monitor in any JVM GUI tool.

Related