Scheduled如何會(huì)在上次任務(wù)執(zhí)行完才會(huì)執(zhí)行下次任務(wù)
Scheduled會(huì)在上次任務(wù)執(zhí)行完才會(huì)執(zhí)行下次任務(wù)
在使用spring的Scheduled定時(shí)任務(wù)
擔(dān)心同一任務(wù),
第一次開(kāi)始未執(zhí)行完就執(zhí)行第二次任務(wù),所以給加了synchronized,
但是后面經(jīng)過(guò)測(cè)試Scheduled定時(shí)任務(wù)會(huì)在上次任務(wù)結(jié)束時(shí)再執(zhí)行第二次任務(wù),
如果第二次任務(wù)堵在哪里了,時(shí)間會(huì)順延
package xyz.hashdog.job; import lombok.AllArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; /** * @author th * @description: 定時(shí)任務(wù)測(cè)試 * @projectName hashdog-ds * @date 2020/2/1223:07 */ @Component @Slf4j @AllArgsConstructor public class TestCrontab { private static final Object KEY = new Object(); private static boolean taskFlag = false; @Scheduled(cron = "*/5 * * * * ?") public void pushCancel() { System.out.println("進(jìn)來(lái)了"); synchronized (KEY) { if (TestCrontab.taskFlag) { System.out.println("測(cè)試調(diào)度已經(jīng)啟動(dòng)"); log.warn("測(cè)試調(diào)度已經(jīng)啟動(dòng)"); return; } TestCrontab.taskFlag = true; } try { for (int i =0;i<=10;i++){ System.out.println("執(zhí)行:"+i); Thread.sleep(2000); } } catch (Exception e) { log.error("測(cè)試調(diào)度執(zhí)行出錯(cuò)", e); } TestCrontab.taskFlag = false; log.warn("測(cè)試調(diào)度執(zhí)行完成"); } }
注釋掉synchronized
執(zhí)行效果一樣,并沒(méi)有線程安全問(wèn)題
package xyz.hashdog.job; import lombok.AllArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; /** * @author th * @description: 定時(shí)任務(wù)測(cè)試 * @projectName hashdog-ds * @date 2020/2/1223:07 */ @Component @Slf4j @AllArgsConstructor public class TestCrontab { private static final Object KEY = new Object(); private static boolean taskFlag = false; @Scheduled(cron = "*/5 * * * * ?") public void pushCancel() throws InterruptedException { System.out.println("進(jìn)來(lái)了"); // synchronized (KEY) { // if (TestCrontab.taskFlag) { // System.out.println("測(cè)試調(diào)度已經(jīng)啟動(dòng)"); // log.warn("測(cè)試調(diào)度已經(jīng)啟動(dòng)"); // return; // } // TestCrontab.taskFlag = true; // } // // try { for (int i =0;i<=10;i++){ System.out.println("執(zhí)行:"+i); Thread.sleep(2000); } // } catch (Exception e) { // log.error("測(cè)試調(diào)度執(zhí)行出錯(cuò)", e); // } // // TestCrontab.taskFlag = false; log.warn("測(cè)試調(diào)度執(zhí)行完成"); } }
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java兩個(gè)乒乓球隊(duì)比賽名單問(wèn)題(判斷素?cái)?shù))
兩個(gè)乒乓球隊(duì)進(jìn)行比賽,各出三人。甲隊(duì)為a,b,c三人,乙隊(duì)為x,y,z三人。已抽簽決定比賽名單。有人向隊(duì)員打聽(tīng)比賽的名單。a說(shuō)他不和x比,c說(shuō)他不和x,z比,請(qǐng)編程序找出三隊(duì)賽手的名單2017-02-02MyBatis-Plus數(shù)據(jù)庫(kù)配置與數(shù)據(jù)源整合方案
本文詳細(xì)介紹了在MyBatis-Plus中進(jìn)行數(shù)據(jù)庫(kù)配置與數(shù)據(jù)源整合的常見(jiàn)方法,包括單數(shù)據(jù)源和多數(shù)據(jù)源的配置步驟,以及如何使用SpringBoot的自動(dòng)配置和手動(dòng)配置來(lái)管理數(shù)據(jù)源,通過(guò)合理的配置,開(kāi)發(fā)者可以簡(jiǎn)化數(shù)據(jù)庫(kù)操作,實(shí)現(xiàn)高效的數(shù)據(jù)庫(kù)管理和復(fù)雜的應(yīng)用架構(gòu)2025-02-02java操作(DOM、SAX、JDOM、DOM4J)xml方式的四種比較與詳解
java中四種操作(DOM、SAX、JDOM、DOM4J)xml方式的比較與詳解2008-10-10spring boot 開(kāi)發(fā)soap webservice的實(shí)現(xiàn)代碼
這篇文章主要介紹了spring boot 開(kāi)發(fā)soap webservice的實(shí)現(xiàn)代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-01-01