What is the difference between corePoolSize and maxPoolSize in Spring ThreadPoolTaskExecutor

I need to send massEmails to all website users. I want to use a thread pool for every message sent. I have currently set the values:

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

What is the difference between them and whether it will scale. I currently have approx. 10,000 users.

+35
java spring multithreading
Dec 10 '09 at 5:32
source share
5 answers

Javadoc says this is better :

When a new task is presented [...], and less than corePoolSize threads a new thread is created, created to handle the request, even if other worker threads are idle. If there is more than corePoolSize , but less than maximumPoolSize thread works, and a new thread will be created only if the queue is full. By setting corePoolSize and maximumPoolSize the same, you create a fixed thread pool size. By setting maximumPoolSize to a substantially unlimited value, such as Integer.MAX_VALUE , you allow the pool for the number of simultaneous tasks.

As for your specific situation, sending 500 letters at the same time is pointless; you just reboot the mail server. If you need to send a large number of letters, use one stream and send them one channel at a time. The mail server will handle this much more gracefully than 500 separate connections.

+39
Dec 10 '09 at 8:30
source share

Here are the Sun rules for creating threads in simple terms:

  1. If the number of threads is less than corePoolSize , create a new corePoolSize to start a new task.
  2. If the number of threads is equal to (or greater than) corePoolSize , put the task in the queue.
  3. If the queue is full and the number of threads is less than maxPoolSize , create a new thread to start the tasks.
  4. If the queue is full and the number of threads is greater than or equal to maxPoolSize , reject the task.

Full article

Origin of response

+23
Jun 14 '17 at 9:43 on
source share

corePoolSize - the minimum number of threads used by the pool. The number may increase to maxPoolSize . When loading decreases, the pool will revert to corePoolSize .

Sending email seems to be related to I / O. I do not think that 500 threads will do it faster.

+19
Dec 10 '09 at 5:41
source share

You should consider increasing the value of queueCapacity than consider increasing the value of corePoolSize or maxPoolSize . These two properties (* PoolSize) are the number of pools that will be executed, but each message will be considered in queueCapacity

 <property name="corePoolSize" value="5" /> <property name="maxPoolSize" value="10" /> <property name="queueCapacity" value="1000" /> <property name="waitForTasksToCompleteOnShutdown" value="true"/> 

If you have 10,000 users to send 1000 * 10 (maxPoolSize) = 10000, but if 1000 is heavy for each thread, we can consider increasing poolSize.

+4
Mar 26 '14 at 12:27
source share

That everything is so clearly explained is correct. A few things to keep in mind are that you should always have a limited size for both the main pool and the queue. If the size of the base pool is very large, it is likely that many of your threads from the pool will not be used for a certain period of time, because for each request a new thread is created until it reaches the maximum pool size

But if your machine will simultaneously process a large number of requests, you should also consider that its size is sufficient enough: example:

If your computer is 1 GB in size and the queue capacity is Integer.MAX_VALUE, then there is a high probability that your machine will at some point in time reject the request due to OutOfMemory, which you can track in any JVM GUI tool.

0
Aug 15 '19 at 19:16
source share



All Articles