SpringBoot定時(shí)任務(wù)多線程實(shí)現(xiàn)示例
測(cè)試Spring Boot定時(shí)任務(wù)沖突時(shí),使用的線程數(shù)量
引入依賴:
Spring Boot 2.6.1
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
簡(jiǎn)單的測(cè)試類
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("做點(diǎn)什么");
}
}
啟動(dòng)類開啟定時(shí)任務(wù):@EnableScheduling
測(cè)試結(jié)果如下:

由結(jié)果可見,main線程啟動(dòng)以后和scheduling-1線程跑了兩個(gè)任務(wù)
由此可見,Spring Boot 定時(shí)器 默認(rèn)是 單線程
我們新增兩個(gè)定時(shí)任務(wù)來看看這樣會(huì)有什么后果
@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 : 做點(diǎn)什么 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 : 做點(diǎn)什么 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 : 做點(diǎn)什么 2021-12-22 16:30:45.006 INFO 14520 --- [ scheduling-1] c.e.springbootscheduled.scheduled.Task : 做點(diǎn)什么 2021-12-22 16:30:45.006 INFO 14520 --- [ scheduling-1] c.e.springbootscheduled.scheduled.Task : 做點(diǎn)什么 2021-12-22 16:30:45.006 INFO 14520 --- [ scheduling-1] c.e.springbootscheduled.scheduled.Task : 做點(diǎn)什么 2021-12-22 16:30:45.006 INFO 14520 --- [ scheduling-1] c.e.springbootscheduled.scheduled.Task : 做點(diǎn)什么 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 : 做點(diǎn)什么 2021-12-22 16:30:55.006 INFO 14520 --- [ scheduling-1] c.e.springbootscheduled.scheduled.Task : 做點(diǎn)什么 2021-12-22 16:30:55.007 INFO 14520 --- [ scheduling-1] c.e.springbootscheduled.scheduled.Task : do something
很明顯任務(wù)會(huì)發(fā)生錯(cuò)亂,嚴(yán)重時(shí)會(huì)導(dǎo)致線程阻塞,最后崩潰
解決方法:引入線程池
新增配置類,配置線程池
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 {
/**
* 默認(rèn)線程數(shù)
*/
private static final int corePoolSize = 10;
/**
* 最大線程數(shù)
*/
private static final int maxPoolSize = 100;
/**
* 允許線程空閑時(shí)間(單位:默認(rèn)為秒),十秒后就把線程關(guān)閉
*/
private static final int keepAliveTime = 10;
/**
* 緩沖隊(duì)列數(shù)
*/
private static final int queueCapacity = 200;
/**
* 線程池名前綴
*/
private static final String threadNamePrefix = "task-thread-";
/**
* bean的名稱,默認(rèn)為 首字小寫 方法名
*/
@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;
}
}
每個(gè)定時(shí)任務(wù)添加注解 @Async("taskExecutor")
@Async("taskExecutor")
@Scheduled(fixedRate = 2000)
public void currentTime(){
log.info("做點(diǎn)什么");
}
啟動(dòng)測(cè)試
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 : 做點(diǎn)什么 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 : 做點(diǎn)什么 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 : 做點(diǎn)什么 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 : 做點(diǎn)什么 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 : 做點(diǎn)什么 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 : 做點(diǎn)什么 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
到此這篇關(guān)于SpringBoot定時(shí)任務(wù)多線程實(shí)現(xiàn)示例的文章就介紹到這了,更多相關(guān)SpringBoot定時(shí)任務(wù)多線程內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
GateWay動(dòng)態(tài)路由與負(fù)載均衡詳細(xì)介紹
這篇文章主要介紹了GateWay動(dòng)態(tài)路由與負(fù)載均衡,GateWay支持自動(dòng)從注冊(cè)中心中獲取服務(wù)列表并訪問,即所謂的動(dòng)態(tài)路由2022-11-11
Intellij無法創(chuàng)建java文件解決方案
這篇文章主要介紹了Intellij無法創(chuàng)建java文件解決方案,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-10-10
詳解Kotlin:forEach也能break和continue
這篇文章主要介紹了詳解Kotlin:forEach也能break和continue的相關(guān)資料,需要的朋友可以參考下2017-06-06
Java8新特性之泛型的目標(biāo)類型推斷_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
泛型是Java SE 1.5的新特性,泛型的本質(zhì)是參數(shù)化類型,也就是說所操作的數(shù)據(jù)類型被指定為一個(gè)參數(shù)。下面通過本文給分享Java8新特性之泛型的目標(biāo)類型推斷,感興趣的朋友參考下吧2017-06-06
ssm框架+PageHelper插件實(shí)現(xiàn)分頁查詢功能
今天小編教大家如何通過ssm框架+PageHelper插件實(shí)現(xiàn)分頁查詢功能,首先大家需要新建一個(gè)maven工程引入jar包,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2021-06-06
Spring Security自定義認(rèn)證器的實(shí)現(xiàn)代碼
這篇文章主要介紹了Spring Security自定義認(rèn)證器的實(shí)現(xiàn)代碼,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-06-06
java注解之運(yùn)行時(shí)修改字段的注解值操作
這篇文章主要介紹了java注解之運(yùn)行時(shí)修改字段的注解值操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-08-08

