Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java spring boot中默认的调度程序池大小是多少?_Java_Spring_Spring Boot - Fatal编程技术网

Java spring boot中默认的调度程序池大小是多少?

Java spring boot中默认的调度程序池大小是多少?,java,spring,spring-boot,Java,Spring,Spring Boot,我正在使用springboot和@Scheduled注释来执行一些任务 在spring boot中,如何确定计划任务的默认池大小 原因:下面的类不是并行执行作业,而是一个接一个地执行作业。默认情况下可能只配置了一个线程执行器 @Service public class ZipFileTesterAsync { @Scheduled(fixedDelay = 60000, initialDelay = 500) public void run() throws Exception

我正在使用
springboot
@Scheduled
注释来执行一些任务

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

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

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

@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的线程池。

一种非常简单的方法:

@配置
公共类ScheduleConfig{
ScheduleConfig(ThreadPoolTaskScheduler ThreadPoolTaskScheduler){
设置池大小(10);
}
}

spring boot中的默认调度程序池大小为只有一个

org.springframework.scheduling.config.ScheduledTaskRegistrar
中:

/**
*根据基础任务计划所有已注册的任务
*{@linkplain#setTaskScheduler(TaskScheduler)任务调度器}。
*/
@抑制警告(“弃用”)
受保护的无效scheduleTasks(){
if(this.taskScheduler==null){
this.localExecutor=Executors.newSingleThreadScheduledExecutor();
this.taskScheduler=新的ConcurrentTaskScheduler(this.localExecutor);
}
...
}

默认池大小为1,您可以通过更改
spring.task.scheduling.pool.size
的值在
application.properties
science springboot2.1.0中设置池大小

spring.task.scheduling.pool.size=20

当触发周期短于执行持续时间时,将序列化执行相同的任务。Spring Boot将以最多20个线程并行执行不同的任务。

使用内置功能和一个稳定的Spring配置,如下所示:

@Bean
公共任务调度器任务调度器(){
返回新的ConcurrentTaskScheduler(新的ScheduledThreadPoolExecutor(20));
}

这可能有助于解决您的问题。对于不同的@Scheduled annotation,是否可能有不同的AD池?可以创建不同的ThreadPoolTaskScheduler,并使用ThreadPoolTaskScheduler.schedule方法。@Ashikaumagaumagiliya不,这是不可能的@Scheduled将始终使用默认的任务计划程序(您可以查看
ScheduledAnnotationBeanPostProcessor
)。为了能够将任务调度到不同的线程池,只需使用内置的Java
Java.util.concurrent.Executors
创建另一个池。如果在同一类中使用@EnableSchedulingI启用它,则无法工作。起初,我很难找到此属性的文档,这里有一个链接,供其他想确认这一点的人使用:官方文件: