Table of contents
Rules for thread pool creation
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 thread count exceeds
queueCapacity
, and the number of threads is less than themaxPoolSize
, 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.
Example
Question:
How threads will be created with below configuration:
Answer:
Snippet
{
executor.setCorePoolSize(100);
executor.setQueueCapacity(10000);
executor.setMaxPoolSize(250);
executor.setThreadNamePrefix("asyncThreadPool-");
executor.initialize();
return executor;
}