繁体   English   中英

弹簧引导启动线程同时用于计划服务

[英]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()));
}

我的ThreadPoolTask​​Executor看起来像这样:

@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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM