[英]Spring-boot starting threads simultaneously for scheduled services
I have a single component which scheduled with a fixedDealy [ 20 second ]. 我有一个单独的组件,它的调度时间为fixedDealy [20秒]。 Sample code snippet is as follows : 示例代码段如下:
@Scheduled(fixedDelayString = "20000")
@Async("specificTaskExecutor")
public void scheduleTaskWithFixedDelay() {
logger.info("Fixed Dealy Task :: Execution Time - {}",
dateTimeFormatter.format(LocalDateTime.now()));
}
My ThreadPoolTaskExecutor looks like this: 我的ThreadPoolTaskExecutor看起来像这样:
@Bean(name = "specificTaskExecutor")
public TaskExecutor specificTaskExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(10);
executor.setMaxPoolSize(10);
executor.setThreadNamePrefix("test-");
executor.initialize();
return executor;
}
When i run my application, i see the result as follows : 运行应用程序时,结果如下:
2019-01-23 23:29:48.084 INFO 47607 --- [ test-1] c.e.s.initial_code.ScheduledTasks : Fixed Delay Task :: Execution Time - 23:29:48
2019-01-23 23:30:08.068 INFO 47607 --- [ test-2] c.e.s.initial_code.ScheduledTasks : Fixed Delay Task :: Execution Time - 23:30:08
2019-01-23 23:30:28.074 INFO 47607 --- [ test-3] c.e.s.initial_code.ScheduledTasks : Fixed Delay Task :: Execution Time - 23:30:28
2019-01-23 23:30:48.080 INFO 47607 --- [ test-4] c.e.s.initial_code.ScheduledTasks : Fixed Delay Task :: Execution Time - 23:30:48
2019-01-23 23:31:08.083 INFO 47607 --- [ test-5] c.e.s.initial_code.ScheduledTasks : Fixed Delay Task :: Execution Time - 23:31:08
2019-01-23 23:31:28.084 INFO 47607 --- [ test-6] c.e.s.initial_code.ScheduledTasks : Fixed Delay Task :: Execution Time - 23:31:28
2019-01-23 23:31:48.087 INFO 47607 --- [ test-7] c.e.s.initial_code.ScheduledTasks : Fixed Delay Task :: Execution Time - 23:31:48
2019-01-23 23:32:08.091 INFO 47607 --- [ test-8] c.e.s.initial_code.ScheduledTasks : Fixed Delay Task :: Execution Time - 23:32:08
2019-01-23 23:32:28.092 INFO 47607 --- [ test-9] c.e.s.initial_code.ScheduledTasks : Fixed Delay Task :: Execution Time - 23:32:28
2019-01-23 23:32:48.092 INFO 47607 --- [ test-10] c.e.s.initial_code.ScheduledTasks : Fixed Delay Task :: Execution Time - 23:32:48
2019-01-23 23:33:08.098 INFO 47607 --- [ test-1] c.e.s.initial_code.ScheduledTasks : Fixed Delay Task :: Execution Time - 23:33:08
Looks like the threads are being fired up with a delay of 20 seconds. 似乎正在以20秒的延迟启动线程。 Is there any way i can start off all the threads at once? 有什么办法可以立即启动所有线程吗?
Could anyone let me know how this can be achieved? 谁能让我知道如何实现?
I think this is what you are asking for 我想这就是你要的
@Scheduled(fixedRate = 20000)
@Async("specificTaskExecutor")
public void scheduleTaskWithFixedDelay() {
logger.info("Fixed Dealy Task :: Execution Time - {}",
dateTimeFormatter.format(LocalDateTime.now()));
}
Instead of using fixedDelayString property use fixedRate. 而不是使用fixedDelayString属性,而是使用fixedRate。
The fixedDelay property makes sure that there is a delay of n millisecond between the finish time of an execution of a task and the start time of the next execution of the task. fixedDelay属性确保在任务执行的完成时间与任务下一次执行的开始时间之间存在n毫秒的延迟。
While the fixedRate property runs the scheduled task at every n millisecond. fixedRate属性每n毫秒运行一次计划的任务。 It doesn't check for any previous executions of the task. 它不检查任务的任何先前执行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.