javaspringspring-async

which default pool type of ThreadPoolTaskExecutor in spring


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?


Solution

  • For pure spring application , the default TaskExecutor will be resolved in the following orders (source codes here) :

    1. The bean that is org.springframework.core.task.TaskExecutor
    2. The bean that is java.util.concurrent.Executor and with the name taskExecutor
    3. 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)