我有一个单独的组件,它安排了20秒的fixedDealy。示例代码片段如下:
@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秒。有没有办法让我一次启动所有的线程?
有人能告诉我如何实现这一点吗?
发布于 2019-02-08 19:07:18
我想这就是你想要的
@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毫秒运行一次计划任务。它不会检查任务以前的任何执行情况。
https://stackoverflow.com/questions/54341659
复制相似问题