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个线程会同时执行,和方法一的结果一致。