Spring Schedule定时任务多线程执行任务配置

Posted liangmingming

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring Schedule定时任务多线程执行任务配置相关的知识,希望对你有一定的参考价值。

@Configuration
public class ScheduleConfig implements SchedulingConfigurer {
    @Override
    public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
        Method[] methods = BatchProperties.Job.class.getMethods();
        int defaultPoolSize = 10;
        int corePoolSize = 0;
        if (methods != null && methods.length > 0) {
            for (Method method : methods) {
                Scheduled annotation = method.getAnnotation(Scheduled.class);
                if (annotation != null) {
                    corePoolSize++;
                }
            }
            if (defaultPoolSize > corePoolSize)
                corePoolSize = defaultPoolSize;
        }
        taskRegistrar.setScheduler(Executors.newScheduledThreadPool(corePoolSize));
    }
}
@Configuration
public class ScheduleConfig implements SchedulingConfigurer {
    @Override
    public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
        Method[] methods = BatchProperties.Job.class.getMethods();
        int defaultPoolSize = 10;
        int corePoolSize = 0;
        if (methods != null && methods.length > 0) {
            for (Method method : methods) {
                Scheduled annotation = method.getAnnotation(Scheduled.class);
                if (annotation != null) {
                    corePoolSize++;
                }
            }
            if (defaultPoolSize > corePoolSize)
                corePoolSize = defaultPoolSize;
        }
        taskRegistrar.setScheduler(Executors.newScheduledThreadPool(corePoolSize));
    }
}
@EnableScheduling

以上是关于Spring Schedule定时任务多线程执行任务配置的主要内容,如果未能解决你的问题,请参考以下文章

Java多线程学习——任务定时调度

Spring Boot 中配置定时任务,实现多线程操作

@Schedule定时任务是并行执行吗

@Schedule定时任务是并行执行吗

Spring Boot定时任务运行一段时间后自动关闭的解决办法

Java多线程 定时器