[英]Spring-boot starting threads simultaneously for scheduled services
我有一個單獨的組件,它的調度時間為fixedDealy [20秒]。 示例代碼段如下:
@Scheduled(fixedDelayString = "20000")
@Async("specificTaskExecutor")
public void scheduleTaskWithFixedDelay() {
logger.info("Fixed Dealy Task :: Execution Time - {}",
dateTimeFormatter.format(LocalDateTime.now()));
}
我的ThreadPoolTaskExecutor看起來像這樣:
@Bean(name = "specificTaskExecutor")
public TaskExecutor specificTaskExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(10);
executor.setMaxPoolSize(10);
executor.setThreadNamePrefix("test-");
executor.initialize();
return executor;
}
運行應用程序時,結果如下:
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
似乎正在以20秒的延遲啟動線程。 有什么辦法可以立即啟動所有線程嗎?
誰能讓我知道如何實現?
我想這就是你要的
@Scheduled(fixedRate = 20000)
@Async("specificTaskExecutor")
public void scheduleTaskWithFixedDelay() {
logger.info("Fixed Dealy Task :: Execution Time - {}",
dateTimeFormatter.format(LocalDateTime.now()));
}
而不是使用fixedDelayString屬性,而是使用fixedRate。
fixedDelay屬性確保在任務執行的完成時間與任務下一次執行的開始時間之間存在n毫秒的延遲。
fixedRate屬性每n毫秒運行一次計划的任務。 它不檢查任務的任何先前執行。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.