1.创建SpringBoot项目
1.1项目结构
1.2导入基本依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
2.两个关键注解
@EnableScheduling:标注在主启动类上,开启定时任务
@Scheduled:标注在方法上,定时任务入口,后面可以自定义定时任务的频率。例如:@Scheduled(cron = "*/1 * * * * ?")
3.代码
3.1主启动类
package com.wangjw;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;
@SpringBootApplication
@EnableScheduling
public class ScheduleApplication {
public static void main(String[] args) {
SpringApplication.run(ScheduleApplication.class, args);
}
}
3.2service层 创建5个定时任务
package com.wangjw.service;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
/**
* @author wangjw
* @date 2021 年 05 月 28 日
*/
@Service
public class ScheduleTask {
@Scheduled(cron = "*/1 * * * * ?")
public void task1(){
System.out.println(Thread.currentThread().getName()+"调度任务1正在执行");
try {
Thread.sleep(3000L);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
@Scheduled(cron = "*/1 * * * * ?")
public void task2(){
System.out.println(Thread.currentThread().getName()+"调度任务2正在执行");
try {
Thread.sleep(3000L);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
@Scheduled(cron = "*/1 * * * * ?")
public void task3(){
System.out.println(Thread.currentThread().getName()+"调度任务3正在执行");
try {
Thread.sleep(3000L);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
@Scheduled(cron = "*/1 * * * * ?")
public void task4(){
System.out.println(Thread.currentThread().getName()+"调度任务4正在执行");
try {
Thread.sleep(3000L);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
@Scheduled(cron = "*/1 * * * * ?")
public void task5(){
System.out.println(Thread.currentThread().getName()+"调度任务5正在执行");
try {
Thread.sleep(3000L);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
3.3开启多线程执行
多个任务时,建议使用多线程,保证多个任务独立运行,互不影响
package com.wangjw.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import java.util.concurrent.Executors;
/**
* @author wangjw
* @date 2021 年 05 月 28 日
*/
@Configuration
public class ScheduleConfig implements SchedulingConfigurer {
@Override
public void configureTasks(ScheduledTaskRegistrar scheduledTaskRegistrar) {
// 配置线程池的大小:需要根据实际的电脑配置来调整
scheduledTaskRegistrar.setScheduler(Executors.newScheduledThreadPool(5));
}
}
4.运行结果
pool-1-thread-2调度任务2正在执行
pool-1-thread-3调度任务3正在执行
pool-1-thread-1调度任务1正在执行
pool-1-thread-4调度任务5正在执行
pool-1-thread-5调度任务4正在执行
pool-1-thread-4调度任务4正在执行
pool-1-thread-5调度任务5正在执行
pool-1-thread-1调度任务3正在执行
pool-1-thread-3调度任务1正在执行
pool-1-thread-2调度任务2正在执行
pool-1-thread-3调度任务1正在执行