Backend Development 9 min read

Using @Scheduled in Spring Boot for Task Scheduling: Configuration, Parameters, and Multi‑threading

This article explains how to create and configure Spring Boot scheduled tasks with @Scheduled, covering property‑based settings, cron expressions, fixed‑rate and fixed‑delay options, the required @EnableScheduling annotation, and how to switch from the default single‑thread executor to a multi‑threaded pool for concurrent execution.

Top Architect
Top Architect
Top Architect
Using @Scheduled in Spring Boot for Task Scheduling: Configuration, Parameters, and Multi‑threading

Spring Boot provides the @Scheduled annotation to define scheduled tasks. The article shows how to configure tasks via application.properties , with examples of fixedDelayString and cron expressions, and emphasizes the necessity of the @EnableScheduling annotation.

package com.accord.task;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
 * Load task information from configuration file
 * @author 王久印
 */
@Component
public class ScheduledTask {
    private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");

    // @Scheduled(fixedDelayString = "${jobs.fixedDelay}")
    @Scheduled(fixedDelayString = "2000")
    public void getTask1() {
        System.out.println("任务1,从配置文件加载任务信息,当前时间:" + dateFormat.format(new Date()));
    }

    @Scheduled(cron = "${jobs.cron}")
    public void getTask2() {
        System.out.println("任务2,从配置文件加载任务信息,当前时间:" + dateFormat.format(new Date()));
    }
}

The corresponding application.properties entries are:

jobs.fixedDelay=5000
jobs.cron=0/5 * *  * * ?

The main application class must enable scheduling:

package com.accord;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;

@SpringBootApplication
@EnableScheduling
public class SpringBootCron2Application {
    public static void main(String[] args) {
        SpringApplication.run(SpringBootCron2Application.class, args);
    }
}

The article lists the most common @Scheduled parameters:

@Scheduled(fixedRate=2000) – execute every 2 seconds after the previous start.

@Scheduled(fixedDelay=2000) – execute 2 seconds after the previous finish.

@Scheduled(initialDelay=1000, fixedDelay=2000) – wait 1 second before the first execution, then use fixed‑delay.

@Scheduled(cron="* * * * * ?") – execute according to a cron expression.

By default Spring Boot creates a single‑threaded executor for all scheduled tasks. To achieve concurrent execution, the article demonstrates implementing SchedulingConfigurer and providing a custom thread pool:

package com.accord.task;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import java.util.concurrent.Executors;

@Configuration
public class ScheduleConfig implements SchedulingConfigurer {
    @Override
    public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
        // Set a thread pool with 10 threads for scheduled tasks
        taskRegistrar.setScheduler(Executors.newScheduledThreadPool(10));
    }
}

With this configuration, each scheduled method runs in a separate thread, as shown by the multi‑threaded execution screenshots. The article also includes a second example ( ScheduledTask2 ) that demonstrates fixedDelay , fixedRate , initialDelay , and cron usages, logging the execution count for each.

package com.accord.task;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component
public class ScheduledTask2 {
    private Logger logger = LoggerFactory.getLogger(ScheduledTask2.class);
    private int fixedDelayCount = 1;
    private int fixedRateCount = 1;
    private int initialDelayCount = 1;
    private int cronCount = 1;

    @Scheduled(fixedDelay = 5000)
    public void testFixDelay() {
        logger.info("===fixedDelay: 第{}次执行方法", fixedDelayCount++);
    }

    @Scheduled(fixedRate = 5000)
    public void testFixedRate() {
        logger.info("===fixedRate: 第{}次执行方法", fixedRateCount++);
    }

    @Scheduled(initialDelay = 1000, fixedRate = 5000)
    public void testInitialDelay() {
        logger.info("===initialDelay: 第{}次执行方法", initialDelayCount++);
    }

    @Scheduled(cron = "0 0/1 * * * ?")
    public void testCron() {
        logger.info("===cron: 第{}次执行方法", cronCount++);
    }
}

Finally, the article reminds readers that without @EnableScheduling the scheduled methods will never be triggered, and it provides links to official documentation and an online cron expression generator.

backendJavaconcurrencySpring Bootcronscheduled tasks
Top Architect
Written by

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.

0 followers
Reader feedback

How this landed with the community

login 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.