SpringBoot定时任务多线程实现示例
作者:EasyChill 发布时间:2021-10-06 08:45:18
标签:SpringBoot,多线程,定时
测试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
0
投稿
猜你喜欢
- 前言日常开发中,我们可能会碰到需要进行防重放与操作幂等的业务,本文记录SpringBoot实现简单防重与幂等防重放,防止数据重复提交操作幂等
- 本文为大家分享两个实例,相信大家一定会喜欢。实例1:随机生成验证码图片并将之输出为一个png文件效果图:import java.awt.Co
- 简介Security 是 Spring 家族中的一个安全管理框架。相比与另外一个安全框架Shiro,它提供了更丰富的功能,社区资源也比Shi
- application.properties有以下这几条数据方法一:@Value注解+@Component建议properties少的时候用
- 加载本地图片在项目目录下创建assets文件夹,再在其文件夹下创建images文件夹,后面将需要的图片复制到其中即可在pubspec.yam
- 本文实例讲述了java Swing组件setBounds()简单用法。分享给大家供大家参考,具体如下:先看API:public void s
- 前言:JSON 是轻量级的数据交换格式,很常用,尤其是在使用 Ajax 时,在后台将数据封装为 JSON 字符串更是常见。之前在做项目的时候
- maven 常见命令配置maven常用命令#创建项目 -D设置参数mvn archetype:generate -DgroupId=cn.d
- 本文实例讲述了Java截取字符串的方法。分享给大家供大家参考。具体实现方法如下:public static void main(String
- 1 什么是class对象类是程序的一部分,每个类都有一个class对象。换言之,每当编写并且编译了一个新类,就会产生一个class对象(更恰
- 我们知道HashMap集合是允许存放null值的hashMap是根据key的hashCode来寻找存放位置的,那当key为null时, 怎么
- 微服务feign调用添加token1.一般情况是这么配置的具体的怎么调用就不说了 如下配置,就可以在请求头中添加需要的请求头信息。packa
- 现象说明maven的java项目,测试用例和main所在的源码文件均符合缺省写法和格式,但是在使用mvn clean sonar:sonar
- 一:SparkSQL1.SparkSQL简介Spark SQL是Spark的一个模块,用于处理结构化的数据,它提供了一个数据抽象DataFr
- 这篇文章主要介绍了基于Java检查IPv6地址的合法性,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋
- 什么是面向对象Java语言是一个纯面向对象的语言,面向对象的语言不仅只有Java,包括C++,PHP等面向对象的编程思想简称 OOP(Obj
- 本文实例讲述了Java常用内置注解。分享给大家供大家参考,具体如下:一 通过@SuppressWarnings关闭警告信息1 代码publi
- 本文汇总了Spring的常用注解,以方便大家查询和使用,具体如下:使用注解之前要开启自动扫描功能其中base-package为需要扫描的包(
- 前言:2022年Java将有什么新的特性和改进,我相信很多Java开发者都想知道。结合Java语言架构师布莱恩·格茨(
- springboot 异常与重定向在spring中,有两个重定向类型:301,永久性跳转302,暂时性跳转默认调用302。1.下面先通过一个