我有一个组件,该组件以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秒的延迟触发线程。我有什么办法可以立即启动所有线程?
有人可以让我知道如何实现吗?
答案 0 :(得分:0)
我想这就是你要的
@Scheduled(fixedRate = 20000)
@Async("specificTaskExecutor")
public void scheduleTaskWithFixedDelay() {
logger.info("Fixed Dealy Task :: Execution Time - {}",
dateTimeFormatter.format(LocalDateTime.now()));
}
使用fixedRate代替使用fixedDelayString属性。
fixedDelay属性确保在任务执行的完成时间与任务下一次执行的开始时间之间存在n毫秒的延迟。
虽然fixedRate属性每n毫秒运行一次计划的任务。它不会检查任务的先前执行情况。