Spring @ Scheduled scheduled tasks. What if the tasks are too long and lead to overlap?

for example, the fixedRate of a scheduled task is 5 seconds, but sometimes the task executes for more than 5 seconds. What about the next execution of the task?

or cron starts to do a task in 5 seconds per minute, but if the task takes more than 1 minute, what about the next execution of the task?

< H2 > suppose that if there is a timed thread pool, will another thread be taken from the thread pool to perform the next execution of the task, so that the two execution of the task can be carried out concurrently? < / H2 >

in fact, from another point of view, we know that there are two ways to start two tasks An and B. one is to put them in the same class

.
       @Component
       Class Tasks{
           @Scheduled(cron="5 */1 * * * ?")
           public void ATask(){
              ...
           }
           
           @Scheduled(cron="5 */1 * * * ?")
           public void BTask(){
              ...
           }
       }
       

one is put in two different classes

       @Component
       Class ATask{
           @Scheduled(cron="5 */1 * * * ?")
           public void Task(){
              ...
           }        
       }
       

       @Component
       Class BTask{
           @Scheduled(cron="5 */1 * * * ?")
           public void Task(){
              ...
           }        
       }

if there is a timed thread pool, then both An and B tasks can be performed concurrently, right? But if the two executions of the same task overlap in time, can they be executed in parallel?

Mar.07,2021

do not want to overlap. Learn about distributed locks, Redis/hazelcast, etc. If you encounter concurrency, you will skip the subsequent business logic directly.


the problem you are talking about should be said to be unreasonable programming. No matter which strategy spring schedule adopts, it will not solve your problem: overlaying new task threads will cause tasks to pile up and then Avalanche; or skip later tasks, resulting in missed execution, so it depends on which situation your own business can accept, and reasonably plan the interval and timeout.

initialize SchedulerFactoryBean the following parameters are related to this:

number of PROP_THREAD_COUNT: threads. Set this parameter, and the overlay will not exceed this parameter.

if you use the quartz timed task implementation, there is no timeout mechanism by default. You can use
similar way to realize the timed task timeout by yourself.

Menu