Search code examples
javaspringmultithreadingthreadpoolmanagedthreadfactory

What happens if my Java ThreadPoolTaskExecutor gets called more than the "maxPoolSize" setting?


I’m using Java 6 with Spring 3.2.11.RELEASE and JBoss 7.1.3.Final. I have this in my Spring application context file

<!--  Manages thirdparty threads -->
<bean id="taskExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
    <property name="threadFactory" ref="thirdpartyThreadFactory"/>
        <property name="maxPoolSize" value=“10” />
        <property name="corePoolSize" value=“5” />
</bean>
<bean id="thirdpartyThreadFactory" class="org.springframework.scheduling.concurrent.CustomizableThreadFactory">
        <constructor-arg value="thirdparty-"/>
</bean>

My question is, if I call the taskExecutor’s execute method 300 times given the above settings, what happens to the other 290 threads when the first 10 get queued up? Do they get dropped or is it just that 10 threads are executed at a time?


Solution

  • maxPoolSize only defines the number of thread which can run simultaneously. So it is a limit on the number of threads your code is going to generate. The rest of the tasks are not dropped but are in queue and gets executed as soon as a thread frees.

    For eg. if you had maxPoolSize to 1 then all the tasks will get executed sequentially. In your case in a batch of 10 depending on the time taken by each task.