<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
在SpringBoot中可以通過@Scheduled來註解定義一個定時任務,但是有時候你可能發現有的定時任務道理時間卻沒有執行,但是又不是每次都不執行,為什麼呢???
下面這段diam定義了一個沒隔10s執行一次的定時任務:
package com.study.practice.schedule; import lombok.extern.slf4j.Slf4j; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; /** * @Description : schedule測試 * @Version : V1.0.0 * @Date : 2021/12/1 11:22 */ @Component @Slf4j public class ScheduleTest { @Scheduled(cron = "0/10 * * * * ?") public void execute() { log.info("Scheduled task is running ... ..."); } }
記得在啟動類或者Configuration類上新增了@EnableScheduling註解。
啟動應用,控制檯每隔10秒列印一條紀錄檔:
2021-12-01 11:27:00.002 INFO 97876 --- [ scheduling-1] c.study.practice.schedule.ScheduleTest : Scheduled task is running ... ...
2021-12-01 11:27:10.001 INFO 97876 --- [ scheduling-1] c.study.practice.schedule.ScheduleTest : Scheduled task is running ... ...
2021-12-01 11:27:20.002 INFO 97876 --- [ scheduling-1] c.study.practice.schedule.ScheduleTest : Scheduled task is running ... ...
2021-12-01 11:27:30.001 INFO 97876 --- [ scheduling-1] c.study.practice.schedule.ScheduleTest : Scheduled task is running ... ...
2021-12-01 11:27:40.002 INFO 97876 --- [ scheduling-1] c.study.practice.schedule.ScheduleTest : Scheduled task is running ... ...
2021-12-01 11:27:50.001 INFO 97876 --- [ scheduling-1] c.study.practice.schedule.ScheduleTest : Scheduled task is running ... ...
2021-12-01 11:28:00.002 INFO 97876 --- [ scheduling-1] c.study.practice.schedule.ScheduleTest : Scheduled task is running ... ...
2021-12-01 11:28:10.002 INFO 97876 --- [ scheduling-1] c.study.practice.schedule.ScheduleTest : Scheduled task is running ... ...
但是,問題才剛剛開始。
在上面的相關程式碼中,我們使用cron表示式指定的定時任務執行時間點從0秒開始,每隔10s執行一次,現在我們再加一個定時任務:
package com.study.practice.schedule; import lombok.extern.slf4j.Slf4j; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; import java.util.concurrent.TimeUnit; /** * @Description : schedule測試 * @Version : V1.0.0 * @Date : 2021/12/1 11:22 */ @Component @Slf4j public class ScheduleTestTwo { @Scheduled(cron = "0/10 * * * * *") public void second() throws InterruptedException { log.info("Second scheduled task is running... ..."); TimeUnit.SECONDS.sleep(5); } }
啟動專案,執行結果如下:
2021-12-01 11:36:30.001 INFO 134576 --- [ scheduling-1] c.study.practice.schedule.ScheduleTest : Scheduled task is running ... ...
2021-12-01 11:36:30.002 INFO 134576 --- [ scheduling-1] c.s.practice.schedule.ScheduleTestTwo : Second scheduled task is running... ...
2021-12-01 11:36:40.002 INFO 134576 --- [ scheduling-1] c.s.practice.schedule.ScheduleTestTwo : Second scheduled task is running... ...
2021-12-01 11:36:45.002 INFO 134576 --- [ scheduling-1] c.study.practice.schedule.ScheduleTest : Scheduled task is running ... ...
2021-12-01 11:36:50.001 INFO 134576 --- [ scheduling-1] c.s.practice.schedule.ScheduleTestTwo : Second scheduled task is running... ...
2021-12-01 11:36:55.002 INFO 134576 --- [ scheduling-1] c.study.practice.schedule.ScheduleTest : Scheduled task is running ... ...
2021-12-01 11:37:00.001 INFO 134576 --- [ scheduling-1] c.s.practice.schedule.ScheduleTestTwo : Second scheduled task is running... ...
2021-12-01 11:37:05.001 INFO 134576 --- [ scheduling-1] c.study.practice.schedule.ScheduleTest : Scheduled task is running ... ...
可以看到任務1和任務2本該都是每個10s執行,但是卻發現只有任務2執行了,任務1卻等待了5s之後才執行。
為了找到原因,我們從@Scheduled註解的原始碼開始分析:
下面是@Scheduled註解的註釋:
* * <p>Processing of {@code @Scheduled} annotations is performed by * registering a {@link ScheduledAnnotationBeanPostProcessor}. This can be * done manually or, more conveniently, through the {@code <task:annotation-driven/>} * element or @{@link EnableScheduling} annotation. *
劃重點, 每一個有@Scheduled註解的方法都會被註冊為一個ScheduledAnnotationBeanPostProcessor, 再接著往下看ScheduledAnnotationBeanPostProcessor:
/** * Set the {@link org.springframework.scheduling.TaskScheduler} that will invoke * the scheduled methods, or a {@link java.util.concurrent.ScheduledExecutorService} * to be wrapped as a TaskScheduler. * <p>If not specified, default scheduler resolution will apply: searching for a * unique {@link TaskScheduler} bean in the context, or for a {@link TaskScheduler} * bean named "taskScheduler" otherwise; the same lookup will also be performed for * a {@link ScheduledExecutorService} bean. If neither of the two is resolvable, * a local single-threaded default scheduler will be created within the registrar. * @see #DEFAULT_TASK_SCHEDULER_BEAN_NAME */ public void setScheduler(Object scheduler) { this.scheduler = scheduler; }
重點來了,注意這句話:
if neither of two is resolvable, a local single-thread default scheduler will be created within in the registrar.
這句話意味著,如果我們不主動設定我們需要的TaskScheduler,SpringBoot會預設使用一個單執行緒的scheduler來處理我們用@Scheduled註解實現的定時任務,到此我們剛才的問題就可以理解了。
因為是單個執行緒執行所有的定時任務,所有task2如果先執行,因為執行中等待了5s,所以task2執行完後,task1接著繼續執行。
搞清楚這個流程後,解決這個問題就很簡單了。
根據剛才註釋的描述,我們只需要提供一個滿足自己需要的TaskScheduler並註冊到context容器中就可以了。
package com.study.practice.config; import org.springframework.context.annotation.Configuration; import org.springframework.scheduling.annotation.SchedulingConfigurer; import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler; import org.springframework.scheduling.config.ScheduledTaskRegistrar; /** * @Description : scheduler設定類 * @Version : V1.0.0 * @Date : 2021/12/1 14:00 */ @Configuration public class ScheduledTaskConfig implements SchedulingConfigurer { @Override public void configureTasks(ScheduledTaskRegistrar taskRegistrar) { ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler(); taskScheduler.setPoolSize(2); taskScheduler.initialize(); taskRegistrar.setTaskScheduler(taskScheduler); } }
上面的程式碼提供了一個執行緒池大小為2的taskScheduler,再次啟動SpringBoot檢視效果。
2021-12-01 14:05:20.001 INFO 39768 --- [TaskScheduler-2] c.s.practice.schedule.ScheduleTestTwo : Second scheduled task is running... ...
2021-12-01 14:05:20.001 INFO 39768 --- [TaskScheduler-1] c.study.practice.schedule.ScheduleTest : Scheduled task is running ... ...
2021-12-01 14:05:30.002 INFO 39768 --- [TaskScheduler-1] c.study.practice.schedule.ScheduleTest : Scheduled task is running ... ...
2021-12-01 14:05:30.002 INFO 39768 --- [TaskScheduler-2] c.s.practice.schedule.ScheduleTestTwo : Second scheduled task is running... ...
2021-12-01 14:05:40.001 INFO 39768 --- [TaskScheduler-1] c.study.practice.schedule.ScheduleTest : Scheduled task is running ... ...
2021-12-01 14:05:40.001 INFO 39768 --- [TaskScheduler-2] c.s.practice.schedule.ScheduleTestTwo : Second scheduled task is running... ...
可以看到,當執行緒池裡有兩個執行緒時,這兩個任務各種按照預定的時間進行觸發,互不影響了。
以上為個人經驗,希望能給大家一個參考,也希望大家多多支援it145.com。
相關文章
<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
综合看Anker超能充系列的性价比很高,并且与不仅和iPhone12/苹果<em>Mac</em>Book很配,而且适合多设备充电需求的日常使用或差旅场景,不管是安卓还是Switch同样也能用得上它,希望这次分享能给准备购入充电器的小伙伴们有所
2021-06-01 09:31:42
除了L4WUDU与吴亦凡已经多次共事,成为了明面上的厂牌成员,吴亦凡还曾带领20XXCLUB全队参加2020年的一场音乐节,这也是20XXCLUB首次全员合照,王嗣尧Turbo、陈彦希Regi、<em>Mac</em> Ova Seas、林渝植等人全部出场。然而让
2021-06-01 09:31:34
目前应用IPFS的机构:1 谷歌<em>浏览器</em>支持IPFS分布式协议 2 万维网 (历史档案博物馆)数据库 3 火狐<em>浏览器</em>支持 IPFS分布式协议 4 EOS 等数字货币数据存储 5 美国国会图书馆,历史资料永久保存在 IPFS 6 加
2021-06-01 09:31:24
开拓者的车机是兼容苹果和<em>安卓</em>,虽然我不怎么用,但确实兼顾了我家人的很多需求:副驾的门板还配有解锁开关,有的时候老婆开车,下车的时候偶尔会忘记解锁,我在副驾驶可以自己开门:第二排设计很好,不仅配置了一个很大的
2021-06-01 09:30:48
不仅是<em>安卓</em>手机,苹果手机的降价力度也是前所未有了,iPhone12也“跳水价”了,发布价是6799元,如今已经跌至5308元,降价幅度超过1400元,最新定价确认了。iPhone12是苹果首款5G手机,同时也是全球首款5nm芯片的智能机,它
2021-06-01 09:30:45