<bean id="threadPoolTaskExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor"> <property name="corePoolSize" value="10" /> <property name="maxPoolSize" value="25" /> <property name="queueCapacity" value="30" /> </bean>
- This will create new thread each time a task comes till 10and will go till 10.
- Then It will keep tasks in queue till 30.
- If queue is full it will create 11th thread and will go till 25.
- Then will throw TaskRejected Exception.
More doc about Spring taskExecutor and scheduler
And for the java concurrent’s ThreadPoolExecutor
A blocking queue is a queue that blocks when you try to dequeue from it and the queue is empty, or if you try to enqueue items to it and the queue is already full. A thread trying to dequeue from an empty queue is blocked until some other thread inserts an item into the queue. A thread trying to enqueue an item in a full queue is blocked until some other thread makes space in the queue, either by dequeuing one or more items or clearing the queue completely.
- ThreadPoolTaskExecutor create threads to up corePoolSize when it’s initiated.
- It takes request up to corePoolSize and let thread to process the task.
- If there are more requests incoming while all threads are busy, ThreadPoolTaskExecutor is start queuing up those request into internal queue. This can be problematic since this queue size will be Integer.MAX_VALUE as default if you don’t specify queue queueCapacity.
- Request added in #3 will be executed by thread when there is any available threads in the pool.
- If requests are keep coming and all threads are busy & queue is full, the ThreadPoolTaskExecutor starts creating new threads up to maxPoolSize to process requests.
- If requests over those (increased threads number + queue size), then task will be rejected or following policy you specified.
This article has a good discussion on this.
This article explains the detail usage of blocking queue