SpringBoot定时任务多线程实现示例
测试Spring Boot定时任务冲突时,使用的线程数量
引入依赖:
Spring Boot 2.6.1
org.projectlombok lombok
简单的测试类
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@Slf4jpublic class Task {@Scheduled(cron="*/2 * * * * ?")public void process(){log.info("do something"); }@Scheduled(fixedRate = 2000)public void currentTime(){log.info("做点什么"); }}
启动类开启定时任务:@EnableScheduling
测试结果如下:
文章图片
由结果可见,main线程启动以后和scheduling-1线程跑了两个任务
【SpringBoot定时任务多线程实现示例】由此可见,
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.735INFO 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.738INFO 14520 --- [main] c.e.s.SpringBootScheduledApplication: No active profile set, falling back to default profiles: default2021-12-22 16:30:29.315INFO 14520 --- [scheduling-1] c.e.springbootscheduled.scheduled.Task: 做点什么2021-12-22 16:30:29.318INFO 14520 --- [main] c.e.s.SpringBootScheduledApplication: Started SpringBootScheduledApplication in 0.937 seconds (JVM running for 3.068)2021-12-22 16:30:30.002INFO 14520 --- [scheduling-1] c.e.springbootscheduled.scheduled.Task: do something2021-12-22 16:30:40.003INFO 14520 --- [scheduling-1] c.e.springbootscheduled.scheduled.Task: do something use long long time2021-12-22 16:30:45.005INFO 14520 --- [scheduling-1] c.e.springbootscheduled.scheduled.Task: do something use long time2021-12-22 16:30:45.005INFO 14520 --- [scheduling-1] c.e.springbootscheduled.scheduled.Task: 做点什么2021-12-22 16:30:45.005INFO 14520 --- [scheduling-1] c.e.springbootscheduled.scheduled.Task: do something2021-12-22 16:30:45.006INFO 14520 --- [scheduling-1] c.e.springbootscheduled.scheduled.Task: 做点什么2021-12-22 16:30:45.006INFO 14520 --- [scheduling-1] c.e.springbootscheduled.scheduled.Task: 做点什么2021-12-22 16:30:45.006INFO 14520 --- [scheduling-1] c.e.springbootscheduled.scheduled.Task: 做点什么2021-12-22 16:30:45.006INFO 14520 --- [scheduling-1] c.e.springbootscheduled.scheduled.Task: 做点什么2021-12-22 16:30:45.006INFO 14520 --- [scheduling-1] c.e.springbootscheduled.scheduled.Task: 做点什么2021-12-22 16:30:55.006INFO 14520 --- [scheduling-1] c.e.springbootscheduled.scheduled.Task: do something use long long time2021-12-22 16:30:55.006INFO 14520 --- [scheduling-1] c.e.springbootscheduled.scheduled.Task: 做点什么2021-12-22 16:30:55.006INFO 14520 --- [scheduling-1] c.e.springbootscheduled.scheduled.Task: 做点什么2021-12-22 16:30:55.007INFO 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@EnableAsyncpublic 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.141INFO 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.143INFO 19424 --- [main] c.e.s.SpringBootScheduledApplication: No active profile set, falling back to default profiles: default2021-12-22 16:41:36.991INFO 19424 --- [main] c.e.s.SpringBootScheduledApplication: Started SpringBootScheduledApplication in 1.325 seconds (JVM running for 3.392)2021-12-22 16:41:37.008INFO 19424 --- [task-thread-1] c.e.springbootscheduled.scheduled.Task: 做点什么2021-12-22 16:41:38.001INFO 19424 --- [task-thread-2] c.e.springbootscheduled.scheduled.Task: do something2021-12-22 16:41:38.991INFO 19424 --- [task-thread-5] c.e.springbootscheduled.scheduled.Task: 做点什么2021-12-22 16:41:40.003INFO 19424 --- [task-thread-8] c.e.springbootscheduled.scheduled.Task: do something2021-12-22 16:41:40.991INFO 19424 --- [task-thread-9] c.e.springbootscheduled.scheduled.Task: 做点什么2021-12-22 16:41:42.001INFO 19424 --- [ task-thread-10] c.e.springbootscheduled.scheduled.Task: do something2021-12-22 16:41:42.989INFO 19424 --- [task-thread-5] c.e.springbootscheduled.scheduled.Task: 做点什么2021-12-22 16:41:43.002INFO 19424 --- [task-thread-3] c.e.springbootscheduled.scheduled.Task: do something use long time2021-12-22 16:41:44.002INFO 19424 --- [task-thread-9] c.e.springbootscheduled.scheduled.Task: do something2021-12-22 16:41:44.990INFO 19424 --- [task-thread-5] c.e.springbootscheduled.scheduled.Task: 做点什么2021-12-22 16:41:45.002INFO 19424 --- [task-thread-6] c.e.springbootscheduled.scheduled.Task: do something use long time2021-12-22 16:41:46.001INFO 19424 --- [task-thread-9] c.e.springbootscheduled.scheduled.Task: do something2021-12-22 16:41:46.990INFO 19424 --- [task-thread-6] c.e.springbootscheduled.scheduled.Task: 做点什么2021-12-22 16:41:47.002INFO 19424 --- [task-thread-1] c.e.springbootscheduled.scheduled.Task: do something use long time2021-12-22 16:41:48.002INFO 19424 --- [task-thread-4] c.e.springbootscheduled.scheduled.Task: do something use long long time
到此这篇关于SpringBoot定时任务多线程实现示例的文章就介绍到这了,更多相关SpringBoot定时任务多线程内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
推荐阅读
- JavaWeb|基于Springboot 和 Mybatis 的后台管理系统
- SpringBoot 整合 RabbitMQ 实现消息可靠传输
- Windows 11自动更新后无任务栏且卡顿问题解决
- 实例7(stc8a8k定时器0,定时50ms,进入中断之后,加数20次到1秒,计算时间。)
- Linux配置定时,使用 crontab -e 与 直接编辑 /etc/crontab 的区别
- activiti流程图在线绘制业务申请审批流转跟进催办,springboot集成工作流基础
- springboot使用redis(从配置到实战)
- springboot-activiti-vue-mysql整合activiti工作流的后端管理系统
- 开始菜单设置处理被任务栏遮住的问题
- 5分钟快速搭建一个springboot的项目