I have a case where I have spring.task.scheduling.pool.size=30
and about 6 tasks with @Scheduled(cron = "0/10 * * * * ?")
, apparently directly relate to 1 task the eventually takes long time to execute, all the other executions of that task is queued in the same thread always, bloating even more the execution of that al other tasks (that eventually are assigned to the same thread).
I'm using Java: Java HotSpot(TM) 64-Bit Server VM 17 Oracle Corporation
Spring Boot: 2.6.4
Edit: some more context, application run in a VM. I actually have 4 VMs running same application, only 1 displaying this behavior. One of the tasks is directly related to data being processed and apparently some data is causing long run of the thread, regardless, the next scheduled tasks will always go to the same thread, it is never executed in any of the other 30 threads, the other 5 task are executed in the other threads. On the nodes I'm not having problem, I see that task being executed in different thread many times (checking through JProfile).
No, there is no common resource dependency on each task.
I'm wondering if, once the next task is put to run, because the previous one might not have finished, scheduler is queuing - could not find any documentation about this, but will test this tomorrow.
CodePudding user response:
First, you need to know how threadPoolExecutor and its parameters work.
The default configuration is a core pool size of 1, with unlimited max pool size and unlimited queue capacity, and this last one - queue capacity - is most important information for you, because new thread is not created when you not exceed this capacity - by default is Integer.MAX_VALUE
.
Try to set this capacity to lower value, for example 2 or 3 and rerun your code.
CodePudding user response:
I'm wondering if, once the next task is put to run, because the previous one might not have finished, scheduler is queuing - could not find any documentation about this
What documentation did you find? The one for @Scheduled#fixedDelay
states
Execute the annotated method with a fixed period between the end of the last invocation and the start of the next.
the duration between the end of the last execution and the start of the next execution is fixed. The task always waits until the previous one is finished.
This option should be used when it’s mandatory that the previous execution is completed before running again.
This is in line with the ScheduledExecutorService
's methods.