What is the default scheduler pool size in spring-boot?

前端 未结 5 1066
栀梦
栀梦 2020-12-05 09:53

I\'m using spring-boot and @Scheduled annotation to execute some tasks.

How can I find out what the default pool size of scheduled tasks is

相关标签:
5条回答
  • 2020-12-05 10:03

    a very simple way to do this:

    @Configuration
    public class ScheduleConfig {
      ScheduleConfig(ThreadPoolTaskScheduler threadPoolTaskScheduler) {
        threadPoolTaskScheduler.setPoolSize(10);
      }
    }
    
    0 讨论(0)
  • 2020-12-05 10:03

    Using built-in capabilities and with anotated spring configuration this will be like that:

        @Bean
        public TaskScheduler taskScheduler() {
            return new ConcurrentTaskScheduler(new ScheduledThreadPoolExecutor(20));
        }
    
    0 讨论(0)
  • 2020-12-05 10:06

    The default scheduler pool size in spring-boot is only one.

    In org.springframework.scheduling.config.ScheduledTaskRegistrar:

        /**
         * Schedule all registered tasks against the underlying
         * {@linkplain #setTaskScheduler(TaskScheduler) task scheduler}.
         */
        @SuppressWarnings("deprecation")
        protected void scheduleTasks() {
            if (this.taskScheduler == null) {
                this.localExecutor = Executors.newSingleThreadScheduledExecutor();
                this.taskScheduler = new ConcurrentTaskScheduler(this.localExecutor);
            }
            ...
        }
    
    
    0 讨论(0)
  • 2020-12-05 10:11

    Yes, all @Scheduled methods share a single thread by default. It is possible to override this behavior by defining a @Configuration such as this:

    @Configuration
    public class SchedulingConfigurerConfiguration implements SchedulingConfigurer {
    
        @Override
        public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
            ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();
            taskScheduler.setPoolSize(100);
            taskScheduler.initialize();
            taskRegistrar.setTaskScheduler(taskScheduler);
        }
    }
    

    This example ensures that all @Scheduled methods share a thread pool of size 100.

    0 讨论(0)
  • 2020-12-05 10:15

    The default pool size is 1, and you can set the pool size in application.properties science springboot2.1.0 via changing the value of spring.task.scheduling.pool.size.

    spring.task.scheduling.pool.size=20
    

    The same task will be executed in serialized when the trigger period is shorter than the execution duration. And Spring Boot will execute different tasks in parallel with a maximum of 20 threads.

    0 讨论(0)
提交回复
热议问题