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 :
&l
The javadoc says it best:
When a new task is submitted [...], and fewer than
corePoolSizethreads are running, a new thread is created to handle the request, even if other worker threads are idle. If there are more thancorePoolSizebut less thanmaximumPoolSizethreads running, a new thread will be created only if the queue is full. By settingcorePoolSizeandmaximumPoolSizethe same, you create a fixed-size thread pool. By settingmaximumPoolSizeto 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.
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.
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.
Here are Sun’s rules for thread creation in simple terms:
corePoolSize, create a new Thread to run a new task.corePoolSize, put the task into the queue.maxPoolSize, create a new thread to run tasks in.maxPoolSize, reject the task.Full article
Origin answer
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:
corePoolSize threads are running, the Executor always prefers adding a new thread rather than queuing.corePoolSize or more threads are running, the Executor always prefers queuing a request rather than adding a new thread.maximumPoolSize, in which case, the task will be rejected.You should consider to increase value of queueCapacity than consider to increase value of corePoolSize or maxPoolSize. Those two properties (*PoolSize) are number of pool to execute but each message would be considering 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 10000 users to send so 1000 * 10 (maxPoolSize) = 10000 but if 1000 for each thread is heavy, we can consider to increase poolSize.