Spring @Scheduled 多线程配置

Spring @Scheduled 多线程配置

环境

  • SpringBoot 2.0.2.RELEASE
  • JDK8

两种方式实现

1.@Scheduled+@Async

测试代码:

@SpringBootApplication
@EnableScheduling
@EnableAsync
public class ScheduleBootstrap {

  public static void main(String[] args) {
    SpringApplication.run(ScheduleBootstrap.class, args);
  }

  @Bean
  public TaskScheduler taskScheduler() {
    ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
    scheduler.setPoolSize(10);
    return scheduler;
  }

  @Scheduled(fixedRate = 100)
  @Async
  public void executeTask() throws InterruptedException {
    Thread.sleep(5000);
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    LocalDateTime date = LocalDateTime.now();
    System.out.println(Thread.currentThread().getName() + ":" + date.format(formatter));
  }

}

执行结果:

taskScheduler-2:2020-12-16 15:14:14
taskScheduler-3:2020-12-16 15:14:14
taskScheduler-1:2020-12-16 15:14:14
taskScheduler-6:2020-12-16 15:14:14
taskScheduler-7:2020-12-16 15:14:14
taskScheduler-4:2020-12-16 15:14:14
taskScheduler-8:2020-12-16 15:14:14
taskScheduler-5:2020-12-16 15:14:14
taskScheduler-9:2020-12-16 15:14:14
taskScheduler-10:2020-12-16 15:14:14

taskScheduler-2:2020-12-16 15:14:19
taskScheduler-3:2020-12-16 15:14:19
taskScheduler-1:2020-12-16 15:14:19
taskScheduler-6:2020-12-16 15:14:19
taskScheduler-7:2020-12-16 15:14:19
taskScheduler-4:2020-12-16 15:14:19
taskScheduler-8:2020-12-16 15:14:19
taskScheduler-5:2020-12-16 15:14:19
taskScheduler-9:2020-12-16 15:14:19
taskScheduler-10:2020-12-16 15:14:19
...

每5秒中,10个线程同时执行

2.Java API

测试代码:

@SpringBootApplication
@EnableScheduling
public class ScheduleBootstrap {

  public static void main(String[] args) {
    SpringApplication.run(ScheduleBootstrap.class, args);
  }

  @Component
  class TaskJob {

    @PostConstruct
    public void init() {
      // 定时线程池
      ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
      scheduler.setPoolSize(10);
      scheduler.initialize();
      for (int i = 0; i < 10; i++) {
        scheduler.scheduleAtFixedRate(this::executeTask, 500);
      }
    }

    public void executeTask() {
      try {
        Thread.sleep(5000);
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime date = LocalDateTime.now();
        System.out.println(Thread.currentThread().getName() + ":" + date.format(formatter));
      } catch (Exception e) {
        e.printStackTrace();
      }
    }
  }

}

执行结果

ThreadPoolTaskScheduler-9:2020-12-16 15:09:26
ThreadPoolTaskScheduler-4:2020-12-16 15:09:26
ThreadPoolTaskScheduler-7:2020-12-16 15:09:26
ThreadPoolTaskScheduler-3:2020-12-16 15:09:26
ThreadPoolTaskScheduler-5:2020-12-16 15:09:26
ThreadPoolTaskScheduler-2:2020-12-16 15:09:26
ThreadPoolTaskScheduler-10:2020-12-16 15:09:26
ThreadPoolTaskScheduler-8:2020-12-16 15:09:26
ThreadPoolTaskScheduler-1:2020-12-16 15:09:26
ThreadPoolTaskScheduler-6:2020-12-16 15:09:26

ThreadPoolTaskScheduler-2:2020-12-16 15:09:31
ThreadPoolTaskScheduler-3:2020-12-16 15:09:31
ThreadPoolTaskScheduler-9:2020-12-16 15:09:31
ThreadPoolTaskScheduler-7:2020-12-16 15:09:31
ThreadPoolTaskScheduler-8:2020-12-16 15:09:31
ThreadPoolTaskScheduler-6:2020-12-16 15:09:31
ThreadPoolTaskScheduler-10:2020-12-16 15:09:31
ThreadPoolTaskScheduler-5:2020-12-16 15:09:31
ThreadPoolTaskScheduler-1:2020-12-16 15:09:31
ThreadPoolTaskScheduler-4:2020-12-16 15:09:31
...

每5秒中,10个线程会同时执行,和方法一的结果一致。

@Scheduled注解是Spring框架提供的用于定时任务的注解。它可以用于标记一个方法,使其在指定的时间间隔或固定的时间点执行。但是,默认情况下,@Scheduled注解只会使用单线程执行任务。 如果你想要使用多线程执行定时任务,可以考虑使用线程池来管理任务的执行。在Spring框架中,可以通过配置TaskExecutor来实现多线程执行定时任务。 首先,你需要在配置文件中配置一个TaskExecutor bean,如下所示: ```java @Configuration @EnableScheduling public class AppConfig implements SchedulingConfigurer { @Override public void configureTasks(ScheduledTaskRegistrar taskRegistrar) { ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler(); taskScheduler.setPoolSize(10); // 设置线程池大小 taskScheduler.initialize(); taskRegistrar.setTaskScheduler(taskScheduler); } @Bean public TaskExecutor taskExecutor() { ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); executor.setCorePoolSize(10); // 设置核心线程 executor.setMaxPoolSize(20); // 设置最大线程 executor.setQueueCapacity(100); // 设置队列容量 executor.setThreadNamePrefix("taskExecutor-"); executor.initialize(); return executor; } } ``` 然后,在你的定时任务方法上使用@Async注解,指定使用TaskExecutor来执行多线程任务,如下所示: ```java @Component public class MyScheduledTasks { @Autowired private TaskExecutor taskExecutor; @Scheduled(fixedRate = 1000) // 每秒执行一次 @Async("taskExecutor") public void task() { // 定时任务逻辑 } } ``` 通过这样的配置,定时任务将会在多个线程中并发执行。你可以根据实际需求,调整线程池的大小和其他参来优化性能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值