Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java ScheduledExecutorService - overlap in cycles

My ScheduledExecutorService is intended to run every 30 minutes - every start and middle of the hour (09:00 , 09:30 etc..). This is set up and runs fine when the task is less than 30 minutes long.

If during one instance of the cycles the execution time is more than 30 minutes what should I expect:

  1. multiple cycles running at the same time (example - the one started at 09:00 may finish at 09:40 but another cycle kicks in at 09:30 and these 2 are running in parallel)
  2. the entire every 30 minutes execution scheme will now move by the additional execution time of the long cycle (example - the one started at 09:00 may finish at 09:40 the one that was supposed to start at 09:30 will start immediately at 09:40).

Many thanks !

like image 357
user3792839 Avatar asked Mar 18 '23 16:03

user3792839


1 Answers

There are two options for scheduling repeating tasks: scheduleAtFixedRate and scheduleWithFixedDelay. I'm assuming you are doing the former, because that's much easier to trigger every 30 minutes.

In which case, the Javadocs on this are quite clear:

If any execution of this task takes longer than its period, then subsequent executions may start late, but will not concurrently execute.

So if your 09:30 task takes 33 minutes, your 10:00 task will slip by 3 minutes.

like image 173
Duncan Jones Avatar answered Apr 01 '23 11:04

Duncan Jones