spring-boot中的默认调度程序池大小是多少? [英] What is the default scheduler pool size in spring-boot?

查看:679
本文介绍了spring-boot中的默认调度程序池大小是多少?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 spring-boot @Scheduled 注释来执行某些任务。

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

如何在spring-boot中找出默认的计划任务默认池大小是什么?

How can I find out what the default pool size of scheduled tasks is by default in spring-boot?

原因:以下class不会并行执行作业,而是一个接一个地执行。也许默认只配置一个线程执行器?

Reason: the following class does not execute the jobs in parallel, but one after the other. Maybe only a single thread executor is configured by default?

@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);
    }
}

结果:第二个作业在第一个完成后执行。

Result: the 2nd job is executed after the first finished.

推荐答案

是的,所有 @Scheduled 方法共享一个线程默认。
可以通过定义 @Configuration 来覆盖此行为,例如:

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);
    }
}

此示例确保所有 @Scheduled 方法共享一个大小为100的线程池。

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

这篇关于spring-boot中的默认调度程序池大小是多少?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆