Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ThreadPoolExecutor - Core and maximum pool sizes [duplicate]

When a new task is submitted in method execute(java.lang.Runnable),and fewer than corePoolSize threads are running, a new thread is created to handle the request, even if other worker threads are idle.

1) Why there is a need to create a new thread to handle the request if there are idle threads?

If there are more than corePoolSize but less than maximumPoolSize threads running, a new thread will be created only if the queue is full.

2) I don't understand the difference between corePoolSize and maximumPoolSize here. Secondly, how can a queue be full when threads are less than maximumPoolSize? Queue can only be full if threads are equal to or more than maximumPoolSize. Isn't it?

like image 940
user2568266 Avatar asked Jul 15 '13 17:07

user2568266


People also ask

What is core pool size and max pool size?

The default configuration of core pool size is 1, max pool size and queue capacity as 2147483647. This is roughly equivalent to Executors. newSingleThreadExecutor(), sharing a single thread for all tasks.

What is difference between corePoolSize and Maxpoolsize?

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 is maximum pool size in java?

To set the maximum pool size: n is the number of connections allowed per pool, from 1 to 2,147,483,647 (the default).


1 Answers

Here are Sun’s rules for thread creation in simple terms:

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

Full article

like image 143
taynguyen Avatar answered Sep 17 '22 17:09

taynguyen