I have read the code but did not find the default pool type of ThreadPoolTaskExecutor. what is the default thread pool of ThreadPoolTaskExecutor? newFixedThreadPool or newCachedThreadPool?
CodePudding user response:
For pure spring application , the default TaskExecutor
will be resolved in the following orders (source codes here) :
- The bean that is
org.springframework.core.task.TaskExecutor
- The bean that is
java.util.concurrent.Executor
and with the nametaskExecutor
SimpleAsyncTaskExecutor
So if you do not configure anything , by default it will use SimpleAsyncTaskExecutor
which internally does not use JDK 's ThreadPoolExecutor
to create a thread. So there is no thread pooling and will create a new thread for each async invocation.
For spring-boot application , it will auto-configure a ThreadPoolTaskExecutor
bean (docs) . As ThreadPoolTaskExecutor
is a type of org.springframework.core.task.TaskExecutor
, it will be used as the default according to the above mentioned order.
ThreadPoolTaskExecutor
internally use JDK 's ThreadPoolExecutor
, but neither its use newFixedThreadPool()
nor newCachedThreadPool()
to create ThreadPoolExecutor
. Rather it directly call its constructor to create but then configure its setting based on the application properties.(source codes here)
CodePudding user response:
You can see the java doc of ThreadPoolTaskExecutor:
From Spring Framework Documentation.
The default configuration is a core pool size of 1, with unlimited max pool size and unlimited queue capacity. This is roughly equivalent to Executors.newSingleThreadExecutor(), sharing a single thread for all tasks.
Setting "queueCapacity" to 0 mimics Executors.newCachedThreadPool(), with > immediate scaling of threads in the pool to a potentially very high number. Consider also setting a "maxPoolSize" at that point, as well as possibly a higher "corePoolSize" (see also the "allowCoreThreadTimeOut" mode of scaling).
The difference between the three executors:
newFixedThreadPool : Creates a thread pool that reuses a fixed number of threads operating off a shared unbounded queue. At any point, at most nThreads threads will be active processing tasks. If additional tasks are submitted when all threads are active, they will wait in the queue until a thread is available. If any thread terminates due to a failure during execution prior to shutdown, a new one will take its place if needed to execute subsequent tasks. The threads in the pool will exist until it is explicitly shutdown.
newSingleThreadExecutor : Creates an Executor that uses a single worker thread operating off an unbounded queue. (Note however that if this single thread terminates due to a failure during execution prior to shutdown, a new one will take its place if needed to execute subsequent tasks.) Tasks are guaranteed to execute sequentially, and no more than one task will be active at any given time. Unlike the otherwise equivalent newFixedThreadPool(1) the returned executor is guaranteed not to be reconfigurable to use additional threads.
newCachedThreadPool : Creates a thread pool that creates new threads as needed, but will reuse previously constructed threads when they are available. These pools will typically improve the performance of programs that execute many short-lived asynchronous tasks. Calls to execute will reuse previously constructed threads if available. If no existing thread is available, a new thread will be created and added to the pool. Threads that have not been used for sixty seconds are terminated and removed from the cache. Thus, a pool that remains idle for long enough will not consume any resources. Note that pools with similar properties but different details (for example, timeout parameters) may be created using ThreadPoolExecutor constructors.