Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

<bean id="myThreadFactory" class="org.springframework.scheduling.concurrent.CustomizableThreadFactory">
    <constructor-arg value="mythread-"/>
</bean>
<bean id="taskExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
    <property name="threadFactory" ref="myThreadFactory"/>
    <property name="corePoolSize" value="10" />
    <property name="maxPoolSize" value="50" />
</bean>

So as not to crush machine CPU usage, I want to limit the amount of concurrent threads that can exist in the system (I assume that's what maxPOolSize does). But I don't want tasks to get dropped. If I add more than 50 tasks to my taskPoolExecutor, what happens to number 51? More importantly, what is the default number of tasks that can be added before they start getting dropped?

like image 807
Dave Avatar asked Jun 19 '17 19:06

Dave


People also ask

How do I find the queue size in ThreadPoolTaskExecutor?

You can just autowire in your ThreadPoolTaskExecutor and get the queue with getThreadPoolExecutor(). getQueue() .

What is core pool size in ThreadPoolTaskExecutor?

The corePoolSize is the minimum number of workers to keep alive without timing out. It is a configurable property of ThreadPoolTaskExecutor. However, the ThreadPoolTaskExecutor abstraction delegates setting this value to the underlying java. util.

What is corePoolSize and max pool size in Threadpoolexecutor?

Starting thread pool size is 1, core pool size is 5, max pool size is 10 and the queue is 100. As requests come in, threads will be created up to 5 and then tasks will be added to the queue until it reaches 100.

What is thread pool queue size?

Thread pool type is fixed with a size of 1 , queue size of 16 .


1 Answers

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, until there are maxPoolSize threads in the pool;
  • when both the pool and the queue are full, new submissions are rejected.
like image 96
kewne Avatar answered Jan 01 '23 07:01

kewne