Resolving Spring Boot Multiple Timer Conflicts with Async and Thread Pool Configuration
This article explains why Spring Boot's default single‑threaded scheduler can cause timer conflicts in real‑world applications and demonstrates how adding @Async annotations together with a custom ThreadPoolTaskExecutor configuration enables concurrent execution of multiple scheduled tasks.
In many real‑world projects there are several scheduled timers that may interfere with each other, especially when a long‑running task blocks the default single‑threaded scheduler.
Typical scenarios include order services with payment timeouts (e.g., Alibaba five‑day, Taobao one‑day, Meituan 15‑minute limits) and fund systems that need to update multiple storage partitions simultaneously.
Spring Boot’s default scheduler runs on a single thread, so if one timer takes a long time, the others are forced to wait, potentially causing a cascade of delays.
The problem can be solved by adding @Async annotations to the scheduled methods and configuring a dedicated thread pool so that each timer can run in its own thread.
Example scheduler implementation:
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.text.SimpleDateFormat;
import java.util.Date;
@Component
public class SchedulerTaskController {
private Logger logger = LoggerFactory.getLogger(SchedulerTaskController.class);
private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
private int count = 0;
@Scheduled(cron = "*/6 * * * * ?")
@Async("threadPoolTaskExecutor")
public void process() {
logger.info("英文:this is scheduler task runing " + (count++));
}
@Scheduled(fixedRate = 6000)
@Async("threadPoolTaskExecutor")
public void currentTime() {
logger.info("中文:现在时间" + dateFormat.format(new Date()));
}
}Corresponding thread‑pool configuration:
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import java.util.concurrent.ThreadPoolExecutor;
@Configuration
@EnableAsync
public class TaskScheduleConfig {
private static final int corePoolSize = 10; // default threads
private static final int maxPoolSize = 100; // max threads
private static final int keepAliveTime = 10; // seconds
private static final int queueCapacity = 200; // queue size
private static final String threadNamePrefix = "it-is-threaddemo-";
@Bean("threadPoolTaskExecutor")
public ThreadPoolTaskExecutor getDemoThread() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(corePoolSize);
executor.setMaxPoolSize(maxPoolSize);
executor.setQueueCapacity(keepAliveTime);
executor.setKeepAliveSeconds(queueCapacity);
executor.setThreadNamePrefix(threadNamePrefix);
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
executor.initialize();
return executor;
}
}After applying this configuration, multiple Spring Boot timers execute concurrently without blocking each other, effectively eliminating the avalanche effect.
The article also contains promotional messages encouraging readers to join a WeChat group for additional resources.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Top Architect
Top Architect focuses on sharing practical architecture knowledge, covering enterprise, system, website, large‑scale distributed, and high‑availability architectures, plus architecture adjustments using internet technologies. We welcome idea‑driven, sharing‑oriented architects to exchange and learn together.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
