Java spring-boot 中的默认调度程序池大小是多少?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29796651/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the default scheduler pool size in spring-boot?
提问by membersound
I'm using spring-boot
and @Scheduled
annotation to execute some tasks.
我正在使用spring-boot
和@Scheduled
注释来执行一些任务。
How can I find out what the default pool size of scheduled tasks is by default in spring-boot?
如何在 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.
结果:第一个作业完成后执行第二个作业。
回答by Arie Z.
Yes, all @Scheduled
methods share a single thread by default.
It is possible to override this behavior by defining a @Configuration
such as this:
是的,@Scheduled
默认情况下所有方法共享一个线程。可以通过定义@Configuration
这样的来覆盖这个行为:
@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.
此示例确保所有@Scheduled
方法共享大小为 100 的线程池。
回答by Bruce
a very simple way to do this:
一个非常简单的方法来做到这一点:
@Configuration
public class ScheduleConfig {
ScheduleConfig(ThreadPoolTaskScheduler threadPoolTaskScheduler) {
threadPoolTaskScheduler.setPoolSize(10);
}
}
回答by Lebecca
The default scheduler pool size in spring-boot is only one.
spring-boot 中的默认调度程序池大小仅为 1。
In org.springframework.scheduling.config.ScheduledTaskRegistrar
:
在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);
}
...
}
回答by robothy
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
.
默认池大小为1,可以在application.properties
science springboot2.1.0 中通过更改 的值来设置池大小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.
当触发周期小于执行持续时间时,相同的任务将被串行执行。并且 Spring Boot 会以最多 20 个线程并行执行不同的任务。