Resolving Spring Boot Timer Conflicts with Multithreaded Scheduling

This article explains why multiple Spring Boot timers can conflict due to single‑threaded execution, demonstrates the problem with example outputs, and shows how to configure a thread pool and use @Async annotations to run timers concurrently, preventing bottlenecks and avalanche effects.

Java Interview Crash Guide
Java Interview Crash Guide
Java Interview Crash Guide
Resolving Spring Boot Timer Conflicts with Multithreaded Scheduling

Tactical Analysis

In real development projects there are often multiple timers, and the main issue is how to avoid conflicts between them.

Use Cases

Our order service has pending orders with time limits (e.g., Alibaba 5 days, Taobao 1 day, Pinduoduo 1 day, Meituan 15 minutes). In a fund system, multiple storage partitions may need simultaneous updates.

Overall, timers in practice must handle concurrent execution and resolve conflicts.

The problem leads to multithreading considerations.

Problem Reproduction

We can clearly see the execution result is "scheduling-1", indicating Spring Boot timers are single‑threaded by default.

If a thread holds a resource for a long time, other timers wait, causing an avalanche of delayed tasks.

Adding a configuration class with annotations solves the issue.

Add Annotations

Specific code:

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("English:this is scheduler task runing " + (count++));
    }

    @Scheduled(fixedRate = 6000)
    @Async("threadPoolTaskExecutor")
    public void currentTime() {
        logger.info("Chinese:现在时间" + dateFormat.format(new Date()));
    }
}

Configuration class:

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;

/** When using multithreading, we often create Thread classes or implement Runnable.
 * Spring provides @EnableAsync support and ThreadPoolTaskExecutor for thread pools.
 */
@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;         // idle time in seconds
    private static final int queueCapacity = 200;       // buffer 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;
    }
}

Running the application shows that timers now execute in separate threads, solving the Spring Boot multi‑timer conflict.

That's all for today.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaSchedulerThreadPoolSpring Bootmultithreading
Java Interview Crash Guide
Written by

Java Interview Crash Guide

Dedicated to sharing Java interview Q&A; follow and reply "java" to receive a free premium Java interview guide.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.