当前位置: 首页 > 知识库问答 >
问题:

同时执行多个Spring@Scheduled任务

翟鹏
2023-03-14

我试图在springboot上同时运行多个计划任务,但实际上它们运行队列(一个接一个,不是并行的)

这是我简单的服务:

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

@Service
public class MyScheduleDemo {

    @Scheduled(fixedDelay = 5000, initialDelay = 1000)
    public void taskA() throws InterruptedException {
        System.out.println("[A] Starting new cycle of scheduled task");

        // Simulate an operation that took 5 seconds.
        long startTime = System.currentTimeMillis();
        while (System.currentTimeMillis() - startTime <= 5000);

        System.out.println("[A] Done the cycle of scheduled task");
    }

    @Scheduled(fixedDelay = 5000, initialDelay = 2000)
    public void taskB() throws InterruptedException {
        System.out.println("[B] Starting new cycle of scheduled task");

        System.out.println("[B] Done the cycle of scheduled task");
    }
}

输出:

[A] Starting new cycle of scheduled task
[A] Done the cycle of scheduled task
[B] Starting new cycle of scheduled task
[B] Done the cycle of scheduled task

但是,它应该是这样的:

[A] Starting new cycle of scheduled task
[B] Starting new cycle of scheduled task
[B] Done the cycle of scheduled task
[A] Done the cycle of scheduled task

我做错了什么?

这是我的配置:

@Configuration
@EnableAsync
@EnableScheduling
public class AsyncConfiguration implements AsyncConfigurer {

    @Override
    @Bean(name = "taskExecutor")
    public Executor getAsyncExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(6);
        executor.setMaxPoolSize(50);
        executor.setQueueCapacity(100);
        executor.setThreadNamePrefix("customer-Executor-");
        executor.initialize();
        return executor;
    }
}

共有2个答案

阎宾实
2023-03-14

或者,您可以在application.properties中配置此属性:

spring.task.scheduling.pool.size=2
曹超
2023-03-14

您应该使用任务计划程序来实现您的目的

@Bean
public ThreadPoolTaskScheduler threadPoolTaskScheduler() {
    ThreadPoolTaskScheduler threadPoolTaskScheduler = new ThreadPoolTaskScheduler();
    threadPoolTaskScheduler.setPoolSize(THREADS_COUNT);
    return threadPoolTaskScheduler;
}

其中THREADS_COUNT-应该并行执行的任务总数。如果我理解正确,你只有2个作业,所以你需要2个线程

 类似资料:
  • 问题内容: 我这里有一个简单的测试方法,该方法设置为每5秒运行一次,但确实可以,但是查看System.out可以看到它似乎在做一些奇怪的事情。 输出: 为什么每次都运行TWICE (出现)? 问题答案: 该注释位于此链接的第25.5.1节下,内容如下: 确保不要在运行时初始化同一@Scheduled注释类的多个实例,除非你确实希望为每个此类实例计划回调。与此相关,请确保不要在使用@Schedule

  • yield 指令可以很简单的将异步控制流以同步的写法表现出来,但与此同时我们将也会需要同时执行多个任务,我们不能直接这样写: // 错误写法,effects 将按照顺序执行 const users = yield call(fetch, '/users'), repos = yield call(fetch, '/repos') 由于第二个 effect 将会在第一个 call 执行完

  • 本文向大家介绍SpringBoot执行定时任务@Scheduled的方法,包括了SpringBoot执行定时任务@Scheduled的方法的使用技巧和注意事项,需要的朋友参考一下 在做项目时,需要一个定时任务来接收数据存入数据库,后端再写一个接口来提供该该数据的最新的那一条。 数据保持最新:设计字段sign的值(0,1)来设定是否最新 定时任务插入数据:首先进行更新,将所有为1即新数据设置过期,然

  • 主要内容:1 如何使用多个线程执行一个任务,2 如何使用多个线程执行多个任务1 如何使用多个线程执行一个任务 如果需要由多个线程执行单个任务,则只有一个run()方法,例如: 1.1 多个线程执行一个任务示例1 输出结果为: 1.2 多个线程执行一个任务示例2 输出结果为: 注意:每个线程在单独的堆栈中运行。 2 如何使用多个线程执行多个任务 如果必须通过多个线程执行多个任务,请使用多个run() 方法: 2.1 多个线程执行多个任务示例1 输出结果为: 2.2 多个线程

  • 我有一个spring boot应用程序,我想有多个方法运行在一天的不同时间。第一个方法运行,但没有后续方法运行。我需要做什么来解决这个问题?这里是我的代码:@enableScheduling@configuration//@conditionalonproperty(name=“spring.enable.scheduling”)@springbootapplication@propertysou

  • 我是spring webflux的新手,我当前的Spring Boot应用程序使用调度程序(注释为@scheduled)从DB读取数据列表,批量并发调用rest api,然后写入事件流,我想在spring webflux中实现同样的操作。 我应该使用@schedule,还是从WebFlux使用scheduleperiotially? 如何将DB中的项目批处理成更小的集合(例如10个项目),并并发调