首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Spring-boot同时启动调度服务的线程

Spring-boot同时启动调度服务的线程
EN

Stack Overflow用户
提问于 2019-01-24 15:47:54
回答 1查看 213关注 0票数 0

我有一个单独的组件,它安排了20秒的fixedDealy。示例代码片段如下:

代码语言:javascript
运行
复制
@Scheduled(fixedDelayString = "20000")
@Async("specificTaskExecutor")
public void scheduleTaskWithFixedDelay() {
    logger.info("Fixed Dealy Task :: Execution Time - {}", 
    dateTimeFormatter.format(LocalDateTime.now()));
}

我的ThreadPoolTaskExecutor看起来像这样:

代码语言:javascript
运行
复制
@Bean(name = "specificTaskExecutor")
public TaskExecutor specificTaskExecutor() {
    ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
    executor.setCorePoolSize(10);
    executor.setMaxPoolSize(10);
    executor.setThreadNamePrefix("test-");
    executor.initialize();
    return executor;
}

当我运行我的应用程序时,我看到如下结果:

代码语言:javascript
运行
复制
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秒。有没有办法让我一次启动所有的线程?

有人能告诉我如何实现这一点吗?

EN

回答 1

Stack Overflow用户

发布于 2019-02-08 19:07:18

我想这就是你想要的

代码语言:javascript
运行
复制
@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毫秒运行一次计划任务。它不会检查任务以前的任何执行情况。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54341659

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档