前言
spingboot项目集成Scheduled定时任务,由于Scheduled默认是单线程定时任务,正常项目使用中,存在多个定时任务同时执行,所以Scheduled执行的时候会出现问题。本文为大家展示Scheduled单线程以及如何使用多线程。
搭建springboot项目,引入spring-boot-starter-web包
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
启动类添加定时任务注解@EnableScheduling
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;
/**
* @author Admin
*/
@EnableScheduling
@SpringBootApplication
public class ScheduledApplication {
public static void main(String[] args) {
SpringApplication.run(ScheduledApplication.class, args);
}
}
创建定时任务的类
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
/**
* @author Admin
*/
@Component
public class MyTimer {
private int num = 0;
@Scheduled(cron = "0/5 * * * * ? ")
public void startSchedule() {
System.out.println(num++);
}
}
日志打印如下
从控制台输出可以看出来,每5秒打印一次,但是这种方式有一个弊端,这种方式使单线程的,当任务并发执行的时候,会出现问题,下面从例子展示一下并发执行时的问题
单线程并发执行
package com.example.scheduled.job;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
/**
* @author Admin
*/
@Component
public class MyTimer {
private int num = 10;
@Scheduled(cron = "0/5 * * * * ? ")
public void ScheduleOne() {
try {
int i = 1;
while (i < num){
System.out.println("任务1===》" + i);
i++;
Thread.sleep(1000);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
@Scheduled(cron = "0/5 * * * * ? ")
public void ScheduleTwo() {
try {
int j = 1;
while (j < num){
System.out.println("任务2===》" + j);
j++;
Thread.sleep(1000);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
日志打印如下
从控制台输出可以看出来,当两个任务同时执行的时候,两个定时任务会顺序执行,只有第一个执行完毕之后,第二个才会执行,在实际的项目开发中,这种是不符合要求的。我们可以通过配置线程池或者异步的方式来实现定时任务的并发执行。
多线程Scheduled方式一
启动类增加Scheduled相关配置,也可以新建一个类,配置Scheduled
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
/**
* @author Admin
*/
@EnableScheduling
@SpringBootApplication
public class ScheduledApplication {
public static void main(String[] args) {
SpringApplication.run(ScheduledApplication.class, args);
}
@Bean
public TaskScheduler taskScheduler() {
ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
scheduler.setPoolSize(10);
return scheduler;
}
}
执行结果如下
多线程Scheduled方式二
启动类增加@EnableAsync注解,定时任务方法增加@Async注解
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
/**
* @author Admin
*/
@EnableScheduling
@SpringBootApplication
@EnableAsync
public class ScheduledApplication {
public static void main(String[] args) {
SpringApplication.run(ScheduledApplication.class, args);
}
}
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
/**
* @author Admin
*/
@Component
public class MyTimer {
private int num = 10;
@Async
@Scheduled(cron = "0/5 * * * * ? ")
public void ScheduleOne() {
try {
int i = 1;
while (i < num){
System.out.println("任务1===》" + i);
i++;
Thread.sleep(1000);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
@Async
@Scheduled(cron = "0/5 * * * * ? ")
public void ScheduleTwo() {
try {
int j = 1;
while (j < num){
System.out.println("任务2===》" + j);
j++;
Thread.sleep(1000);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
执行结果如下
第一次写帖子,如果对大家有用的话,麻烦给个赞,多谢