Java Concurrency Interview

Concurrency in real systems question 135 of 180 mid

Why can one slow @Scheduled job delay every other scheduled job in a Spring app?

Without configuration, Spring Framework schedules on a single-threaded executor, and Spring Boot's auto-configured ThreadPoolTaskScheduler has spring.task.scheduling.pool.size=1. Every @Scheduled method in the application shares that one thread. A report that takes ten minutes means the cache refresh due every minute runs ten minutes late, and nothing in the logs says so.

The fixes, depending on what the jobs are:

Two related facts come up in the follow-up. A single job never overlaps itself: with fixedRate, a run that overruns makes the next one start late, not concurrently. And an exception is logged and the schedule continues, unlike a raw ScheduledExecutorService, where one exception silently cancels every future run. Finally, with several instances of the service, each one runs the job; making it run once per cluster needs a lock outside the JVM, such as ShedLock.

spring scheduling executors

Quiz yourself on Concurrency in real systems Suggest a correction