Understanding the Default Scheduler Pool Size in Spring Boot

Question

What is the default scheduler pool size in Spring Boot?

@Service
public class ZipFileTesterAsync {

    @Scheduled(fixedDelay = 60000, initialDelay = 500)
    public void run() throws Exception {
        System.out.println("import 1");
        TimeUnit.MINUTES.sleep(1);
        System.out.println("import 1 finished");
    }

    @Scheduled(fixedDelay = 60000, initialDelay = 1000)
    public void run2() throws Exception {
        System.out.println("import 2");
        TimeUnit.MINUTES.sleep(1);
    }
}

Answer

In Spring Boot, when using the `@Scheduled` annotation without a specific configuration, tasks by default execute in a single-threaded pool. This means that if one scheduled task is running, others will wait to execute until the current one completes.

@Configuration
public class SchedulerConfig {

    @Bean
    public TaskScheduler taskScheduler() {
        ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
        scheduler.setPoolSize(5); // Set your desired pool size
        scheduler.setThreadNamePrefix("ScheduledTask-");
        return scheduler;
    }
} 

@Service
public class ZipFileTesterAsync {

    @Scheduled(fixedDelay = 60000, initialDelay = 500)
    public void run() throws Exception {
        System.out.println("import 1");
        TimeUnit.MINUTES.sleep(1);
        System.out.println("import 1 finished");
    }

    @Scheduled(fixedDelay = 60000, initialDelay = 1000)
    public void run2() throws Exception {
        System.out.println("import 2");
        TimeUnit.MINUTES.sleep(1);
    }
}

Causes

  • The default thread pool for scheduled tasks is a single-thread executor, which means only one task is executed at a time.
  • If a scheduled method takes a long time to execute, it blocks other scheduled methods from running concurrently.

Solutions

  • To enable parallel execution of scheduled tasks, you can configure a task scheduler with a thread pool size greater than one.
  • You can define a `ThreadPoolTaskScheduler` bean in your configuration class to set a custom thread pool size.

Common Mistakes

Mistake: Not configuring a ThreadPoolTaskScheduler, leading to sequential execution.

Solution: Define a TaskScheduler bean with multiple threads to allow concurrent execution of scheduled tasks.

Mistake: Using long-running tasks within a scheduled method without proper thread management.

Solution: Optimize task execution time or split tasks into smaller, non-blocking calls.

Helpers

  • Spring Boot
  • Scheduler Pool Size
  • Spring Boot @Scheduled
  • Concurrency in Spring Boot
  • Task Scheduler Configuration

Related Questions

⦿Understanding the Difference Between WebDriver's get() and navigate() Methods

Learn the key differences between WebDrivers get and navigate methods their usage and how to wait for page content to load.

⦿How to Read File Bytes in an Android Application

Learn how to read file content as bytes in an Android app including sample code and common mistakes.

⦿How to Convert Strings Between ISO-8859-1 and UTF-8 in Java

Learn how to easily convert strings between ISO88591 and UTF8 encodings in Java preserving special characters throughout the process.

⦿How to Attach Java Runtime Environment (JRE) Source Code in Eclipse?

Learn how to attach JRE source code in Eclipse to view core Java classes like ConcurrentHashMap. Stepbystep guide and code examples included.

⦿How to Log Exceptions Effectively in Java?

Discover best practices for logging exceptions in Java including capturing detailed information for better debugging.

⦿How Do Underscores in Numeric Literals Work in Java 7 and Why Were They Introduced?

Discover how numeric literals with underscores function in Java 7 and the rationale behind their introduction in the JDK.

⦿Why Does the Integer Class Cache Values Ranging from -128 to 127?

Explore the reasons behind the Integer class caching values from 128 to 127 in Java including implications for performance and memory usage.

⦿What is a Percolator in Elasticsearch and How Does It Work?

Discover the concept of percolators in Elasticsearch. Learn how they function their applications and get practical examples for implementation.

⦿How To Implement a Proxy Layer for Spring MVC REST Services?

Learn how to redirect requests through a proxy layer for Spring MVC REST services without redundant serialization.

⦿How to Use Regular Expressions to Remove Content Between XML Tags?

Learn how to remove XML tags and their content using regex with our expert guide. Get stepbystep instructions and example code.

© Copyright 2025 - CodingTechRoom.com