spring ThreadPoolTaskExecutor

<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.

  1. ThreadPoolTaskExecutor create threads to up corePoolSize when it’s initiated.
  2. It takes request up to corePoolSize and let thread to process the task.
  3. 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.
  4. Request added in #3 will be executed by thread when there is any available threads in the pool.
  5. 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.
  6. 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

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s