SpringBoot定時任務(wù)多線程實現(xiàn)示例
測試Spring Boot定時任務(wù)沖突時,使用的線程數(shù)量
引入依賴:
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("做點什么"); } }
啟動類開啟定時任務(wù):@EnableScheduling
測試結(jié)果如下:
由結(jié)果可見,main線程啟動以后和scheduling-1線程跑了兩個任務(wù)
由此可見,Spring Boot 定時器 默認是 單線程
我們新增兩個定時任務(wù)來看看這樣會有什么后果
@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
很明顯任務(wù)會發(fā)生錯亂,嚴重時會導(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 { /** * 默認線程數(shù) */ private static final int corePoolSize = 10; /** * 最大線程數(shù) */ private static final int maxPoolSize = 100; /** * 允許線程空閑時間(單位:默認為秒),十秒后就把線程關(guān)閉 */ private static final int keepAliveTime = 10; /** * 緩沖隊列數(shù) */ 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; } }
每個定時任務(wù)添加注解 @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
到此這篇關(guān)于SpringBoot定時任務(wù)多線程實現(xiàn)示例的文章就介紹到這了,更多相關(guān)SpringBoot定時任務(wù)多線程內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Intellij無法創(chuàng)建java文件解決方案
這篇文章主要介紹了Intellij無法創(chuàng)建java文件解決方案,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-10-10詳解Kotlin:forEach也能break和continue
這篇文章主要介紹了詳解Kotlin:forEach也能break和continue的相關(guān)資料,需要的朋友可以參考下2017-06-06Java8新特性之泛型的目標類型推斷_動力節(jié)點Java學(xué)院整理
泛型是Java SE 1.5的新特性,泛型的本質(zhì)是參數(shù)化類型,也就是說所操作的數(shù)據(jù)類型被指定為一個參數(shù)。下面通過本文給分享Java8新特性之泛型的目標類型推斷,感興趣的朋友參考下吧2017-06-06ssm框架+PageHelper插件實現(xiàn)分頁查詢功能
今天小編教大家如何通過ssm框架+PageHelper插件實現(xiàn)分頁查詢功能,首先大家需要新建一個maven工程引入jar包,本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧2021-06-06Spring Security自定義認證器的實現(xiàn)代碼
這篇文章主要介紹了Spring Security自定義認證器的實現(xiàn)代碼,本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-06-06