Home > Software design >  Will this wait until previous run completes before running again?
Will this wait until previous run completes before running again?

Time:06-08

@Scheduled(cron = "0 * * * * *")
public void doSomething() { ... }

Just wanna make sure this doesn't spin up another run if the previous one doesn't complete in time?

If not, how to make this the case?

I tested with Thread.sleep() and it SEEMS like it's waiting but not sure. I just want to confirm this.

If this means it is only running one at a time does it wait and then immediately run after the previous completes? Or does it skip the run and wait until the next scheduled time when none are running?

CodePudding user response:

Spring use Executors.newSingleThreadScheduledExecutor() to execute cron tasks when no task schduler configured.
Tasks configured by @Scheduled will executed in a ScheduledThreadPoolExecutor with single thread.
ScheduledThreadPoolExecutor use a DelayQueue to schedule tasks.

So, default behavior is tasks will running as serial. It will not skip the run until the task ended. And the next scheduled time is uncertain.

  • Related