I am using a Spring ThreadPoolTaskScheduler to execute an unsynchronized method every 5 minutes. Sometimes the execution of the method takes more than 5 minutes. I have tried to observe the behavior in such a scenario and it appears that the method is not executed again until the current execution is complete (even if the 5 minute interval is over).
Although this suits what I want to accomplish but I am wondering why a second thread is not spawned after 5 minutes to execute the method in parallel to the one that is taking more than 5 minutes. At first I thought this had to do with the pool size which is 1 by default. However, even when I increased the pool size the behavior remains the same.
I tried to go deeper into the ThreadPoolScheduler code and it appears that internally it runs the task via a ThreadPoolExecutor. Is it designed to run only one thread that waits for one execution to complete before firing another one, even if the interval has passed ?
As far as the code is concerned this i what I do –
<task:scheduler id="scheduler" pool-size="1" />
scheduler.scheduleAtFixedRate(new Runnable() {
public void run() {
executeThis(); // this takes more than 5 minutes sometime
}
}, 5*60*1000);
Regards,
Tushar
A second thread is not spawned because it would break the contract of the method, specified in its javadoc:
This behavior is the designed, documented behavior.