What's Spring's default queue size with a ThreadPoolTaskExecutor?

前端 未结 2 1651
花落未央
花落未央 2021-02-20 11:01

I\'m using Spring 4.3.8.RELEASE with Java 7. I want to create a thread pool to execute tasks so I have set up the following in my Spring contxet



        
相关标签:
2条回答
  • 2021-02-20 11:19

    Setting maxPoolSize implicitly allows for tasks to get dropped. However, the default queue capacity is Integer.MAX_VALUE, which, for practical purposes, is infinity.

    Something to watch out for is that ThreadPoolTaskExecutor uses a ThreadPoolExecutor underneath, which has a somewhat unusual approach to queueing, described in the docs:

    If corePoolSize or more threads are running, the Executor always prefers queuing a request rather than adding a new thread.

    This means that maxPoolSize is only relevant when the queue is full, otherwise the number of threads will never grow beyond corePoolSize. As an example, if we submit tasks that never complete to the thread pool:

    • the first corePoolSize submissions will start a new thread each;
    • after that, all submissions go to the queue;
    • if the queue is finite and its capacity is exhausted, each submission starts a new thread, up to maxPoolSize;
    • when both the pool and the queue are full, new submissions are rejected.
    0 讨论(0)
  • 2021-02-20 11:29

    Be aware of using the default ThreadPoolTaskExecutor if many @Async tasks are executed.

    As of Spring Boot 2.1 there is a default ThreadPoolTaskExecutor with a default core size of eight threads. Although the max pool size is still infinity¹ and new threads can be theoretically created the queue size of this pool is infinity¹ as well.

    The queue size will probably never hit and new threads will never be created.

    At least set the queue size if @Async is used:

    spring.task.execution.pool.queue-capacity=16
    

    ¹ infinity as in 2147483647 a.k.a. Integer.MAX_VALUE

    0 讨论(0)
提交回复
热议问题