问题描述
我正在使用 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?
原因:下面的类不是并行执行作业,而是一个接一个地执行.可能默认只配置了单线程执行器?
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 中默认的调度程序池大小是多少?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!