SpringBoot定时任务多线程实现示例
作者:EasyChill 时间:2021-10-06 08:45:18
测试Spring Boot定时任务冲突时,使用的线程数量
引入依赖:
Spring Boot 2.6.1
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
简单的测试类
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
/**
* @author Song Jiangtao
* @date 2021/12/22
* @description:
*/
@Component
@Slf4j
public class Task {
@Scheduled(cron="*/2 * * * * ?")
public void process(){
log.info("do something");
}
@Scheduled(fixedRate = 2000)
public void currentTime(){
log.info("做点什么");
}
}
启动类开启定时任务:@EnableScheduling
测试结果如下:
由结果可见,main线程启动以后和scheduling-1线程跑了两个任务
由此可见,Spring Boot 定时器 默认是 单线程
我们新增两个定时任务来看看这样会有什么后果
@Scheduled(cron="*/2 * * * * ?")
public void process2() throws InterruptedException {
Thread.sleep(5000L);
log.info("do something use long time");
}
@Scheduled(cron="*/2 * * * * ?")
public void process3() throws InterruptedException {
Thread.sleep(10000L);
log.info("do something use long long time");
}
2021-12-22 16:30:28.735 INFO 14520 --- [ main] c.e.s.SpringBootScheduledApplication : Starting SpringBootScheduledApplication using Java 1.8.0_202 on TCZ-D with PID 14520 (D:\code\SpringBootScheduled\target\classes started by Administrator in D:\code\SpringBootScheduled)
2021-12-22 16:30:28.738 INFO 14520 --- [ main] c.e.s.SpringBootScheduledApplication : No active profile set, falling back to default profiles: default
2021-12-22 16:30:29.315 INFO 14520 --- [ scheduling-1] c.e.springbootscheduled.scheduled.Task : 做点什么
2021-12-22 16:30:29.318 INFO 14520 --- [ main] c.e.s.SpringBootScheduledApplication : Started SpringBootScheduledApplication in 0.937 seconds (JVM running for 3.068)
2021-12-22 16:30:30.002 INFO 14520 --- [ scheduling-1] c.e.springbootscheduled.scheduled.Task : do something
2021-12-22 16:30:40.003 INFO 14520 --- [ scheduling-1] c.e.springbootscheduled.scheduled.Task : do something use long long time
2021-12-22 16:30:45.005 INFO 14520 --- [ scheduling-1] c.e.springbootscheduled.scheduled.Task : do something use long time
2021-12-22 16:30:45.005 INFO 14520 --- [ scheduling-1] c.e.springbootscheduled.scheduled.Task : 做点什么
2021-12-22 16:30:45.005 INFO 14520 --- [ scheduling-1] c.e.springbootscheduled.scheduled.Task : do something
2021-12-22 16:30:45.006 INFO 14520 --- [ scheduling-1] c.e.springbootscheduled.scheduled.Task : 做点什么
2021-12-22 16:30:45.006 INFO 14520 --- [ scheduling-1] c.e.springbootscheduled.scheduled.Task : 做点什么
2021-12-22 16:30:45.006 INFO 14520 --- [ scheduling-1] c.e.springbootscheduled.scheduled.Task : 做点什么
2021-12-22 16:30:45.006 INFO 14520 --- [ scheduling-1] c.e.springbootscheduled.scheduled.Task : 做点什么
2021-12-22 16:30:45.006 INFO 14520 --- [ scheduling-1] c.e.springbootscheduled.scheduled.Task : 做点什么
2021-12-22 16:30:55.006 INFO 14520 --- [ scheduling-1] c.e.springbootscheduled.scheduled.Task : do something use long long time
2021-12-22 16:30:55.006 INFO 14520 --- [ scheduling-1] c.e.springbootscheduled.scheduled.Task : 做点什么
2021-12-22 16:30:55.006 INFO 14520 --- [ scheduling-1] c.e.springbootscheduled.scheduled.Task : 做点什么
2021-12-22 16:30:55.007 INFO 14520 --- [ scheduling-1] c.e.springbootscheduled.scheduled.Task : do something
很明显任务会发生错乱,严重时会导致线程阻塞,最后崩溃
解决方法:引入线程池
新增配置类,配置线程池
package com.example.springbootscheduled.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import java.util.concurrent.ThreadPoolExecutor;
/**
* @author Song Jiangtao
* @date 2021/12/22
* @description:
*/
@Configuration
@EnableAsync
public class TaskConfig {
/**
* 默认线程数
*/
private static final int corePoolSize = 10;
/**
* 最大线程数
*/
private static final int maxPoolSize = 100;
/**
* 允许线程空闲时间(单位:默认为秒),十秒后就把线程关闭
*/
private static final int keepAliveTime = 10;
/**
* 缓冲队列数
*/
private static final int queueCapacity = 200;
/**
* 线程池名前缀
*/
private static final String threadNamePrefix = "task-thread-";
/**
* bean的名称,默认为 首字小写 方法名
*/
@Bean("taskExecutor")
public ThreadPoolTaskExecutor getThread() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(corePoolSize);
executor.setMaxPoolSize(maxPoolSize);
executor.setQueueCapacity(keepAliveTime);
executor.setKeepAliveSeconds(queueCapacity);
executor.setThreadNamePrefix(threadNamePrefix);
//线程池拒绝策略
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
executor.initialize();
return executor;
}
}
每个定时任务添加注解 @Async("taskExecutor")
@Async("taskExecutor")
@Scheduled(fixedRate = 2000)
public void currentTime(){
log.info("做点什么");
}
启动测试
2021-12-22 16:41:36.141 INFO 19424 --- [ main] c.e.s.SpringBootScheduledApplication : Starting SpringBootScheduledApplication using Java 1.8.0_202 on TCZ-D with PID 19424 (D:\code\SpringBootScheduled\target\classes started by Administrator in D:\code\SpringBootScheduled)
2021-12-22 16:41:36.143 INFO 19424 --- [ main] c.e.s.SpringBootScheduledApplication : No active profile set, falling back to default profiles: default
2021-12-22 16:41:36.991 INFO 19424 --- [ main] c.e.s.SpringBootScheduledApplication : Started SpringBootScheduledApplication in 1.325 seconds (JVM running for 3.392)
2021-12-22 16:41:37.008 INFO 19424 --- [ task-thread-1] c.e.springbootscheduled.scheduled.Task : 做点什么
2021-12-22 16:41:38.001 INFO 19424 --- [ task-thread-2] c.e.springbootscheduled.scheduled.Task : do something
2021-12-22 16:41:38.991 INFO 19424 --- [ task-thread-5] c.e.springbootscheduled.scheduled.Task : 做点什么
2021-12-22 16:41:40.003 INFO 19424 --- [ task-thread-8] c.e.springbootscheduled.scheduled.Task : do something
2021-12-22 16:41:40.991 INFO 19424 --- [ task-thread-9] c.e.springbootscheduled.scheduled.Task : 做点什么
2021-12-22 16:41:42.001 INFO 19424 --- [ task-thread-10] c.e.springbootscheduled.scheduled.Task : do something
2021-12-22 16:41:42.989 INFO 19424 --- [ task-thread-5] c.e.springbootscheduled.scheduled.Task : 做点什么
2021-12-22 16:41:43.002 INFO 19424 --- [ task-thread-3] c.e.springbootscheduled.scheduled.Task : do something use long time
2021-12-22 16:41:44.002 INFO 19424 --- [ task-thread-9] c.e.springbootscheduled.scheduled.Task : do something
2021-12-22 16:41:44.990 INFO 19424 --- [ task-thread-5] c.e.springbootscheduled.scheduled.Task : 做点什么
2021-12-22 16:41:45.002 INFO 19424 --- [ task-thread-6] c.e.springbootscheduled.scheduled.Task : do something use long time
2021-12-22 16:41:46.001 INFO 19424 --- [ task-thread-9] c.e.springbootscheduled.scheduled.Task : do something
2021-12-22 16:41:46.990 INFO 19424 --- [ task-thread-6] c.e.springbootscheduled.scheduled.Task : 做点什么
2021-12-22 16:41:47.002 INFO 19424 --- [ task-thread-1] c.e.springbootscheduled.scheduled.Task : do something use long time
2021-12-22 16:41:48.002 INFO 19424 --- [ task-thread-4] c.e.springbootscheduled.scheduled.Task : do something use long long time
来源:https://blog.csdn.net/Song_JiangTao/article/details/122088954
标签:SpringBoot,多线程,定时
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
MyBatis通用的10种写法总结大全
2022-08-01 12:04:02
lambda表达式解决java后台分组排序过程解析
2023-11-29 06:03:39
java开发之内部类的用法
2023-02-04 21:30:07
IDEA设置背景为自定义照片的操作方法
2022-12-28 09:13:08
![](https://img.aspxhome.com/file/2023/5/61645_0s.jpg)
深入理解Spring AOP
2023-02-09 15:14:40
深入了解Java ServletContext
2023-11-08 22:36:27
![](https://img.aspxhome.com/file/2023/7/59387_0s.png)
C语言高效编程的几招小技巧
2023-11-02 14:12:50
浅谈java 面对对象(抽象 继承 接口 多态)
2022-04-01 07:23:50
Java使用Preference类保存上一次记录的方法
2023-05-08 01:49:14
java内部类原理与用法详解
2022-12-04 14:41:25
通过FeignClient调用微服务提供的分页对象IPage报错的解决
2022-01-27 20:19:23
![](https://img.aspxhome.com/file/2023/7/63057_0s.png)
浅谈JVM内存溢出原因和解决思路
2023-11-23 12:24:15
![](https://img.aspxhome.com/file/2023/1/59781_0s.jpg)
java实现抽奖功能解析
2021-08-29 16:08:21
java中String字符串删除空格的七种方式
2023-03-11 05:54:24
![](https://img.aspxhome.com/file/2023/1/63311_0s.jpg)
Java RandomAccessFile 指定位置实现文件读取与写入
2023-06-05 17:06:25
从java中调用matlab详细介绍
2023-08-01 14:04:17
![](https://img.aspxhome.com/file/2023/0/59870_0s.jpg)
使用spring框架实现数据库事务处理方式
2022-03-01 14:38:13
![](https://img.aspxhome.com/file/2023/8/62738_0s.png)
Spring boot + mybatis + Vue.js + ElementUI 实现数据的增删改查实例代码(二)
2022-11-14 18:37:22
![](https://img.aspxhome.com/file/2023/4/65724_0s.png)
教你怎么用java一键自动生成数据库文档
2021-08-01 02:34:36
![](https://img.aspxhome.com/file/2023/1/61321_0s.png)
Java特性队列和栈的堵塞原理解析
2023-10-13 14:15:55