Java定時(shí)任務(wù)取消的示例代碼
java定時(shí)任務(wù)如何取消,并比如,我之前想每周二晚上6點(diǎn)自動(dòng)生成一條devops流水線,現(xiàn)在我想停掉
答案: 在Java中,可以使用ScheduledExecutorService
類來創(chuàng)建定時(shí)任務(wù)。要取消定時(shí)任務(wù),可以調(diào)用ScheduledFuture
對(duì)象的cancel()
方法。
以下是一個(gè)示例代碼,演示如何創(chuàng)建一個(gè)每周二晚上6點(diǎn)自動(dòng)生成一條devops流水線的定時(shí)任務(wù),并在需要時(shí)取消它:
import java.util.Calendar; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledFuture; import java.util.concurrent.TimeUnit; public class Main { public static void main(String[] args) { // 創(chuàng)建一個(gè)ScheduledExecutorService對(duì)象 ScheduledExecutorService executor = Executors.newScheduledThreadPool(1); // 獲取當(dāng)前時(shí)間 Calendar now = Calendar.getInstance(); // 計(jì)算下一個(gè)周二晚上6點(diǎn)的時(shí)間 Calendar nextExecutionTime = getNextExecutionTime(now); // 計(jì)算當(dāng)前時(shí)間到下一個(gè)執(zhí)行時(shí)間的時(shí)間間隔 long initialDelay = nextExecutionTime.getTimeInMillis() - now.getTimeInMillis(); // 創(chuàng)建定時(shí)任務(wù) ScheduledFuture<?> future = executor.scheduleAtFixedRate(new Runnable() { @Override public void run() { // 自動(dòng)生成devops流水線的代碼 System.out.println("Generating devops pipeline..."); } }, initialDelay, 7 * 24 * 60 * 60 * 1000, TimeUnit.MILLISECONDS); // 在需要時(shí)取消定時(shí)任務(wù) // future.cancel(true); // 關(guān)閉ScheduledExecutorService // executor.shutdown(); } private static Calendar getNextExecutionTime(Calendar now) { Calendar nextExecutionTime = (Calendar) now.clone(); nextExecutionTime.set(Calendar.DAY_OF_WEEK, Calendar.TUESDAY); nextExecutionTime.set(Calendar.HOUR_OF_DAY, 18); nextExecutionTime.set(Calendar.MINUTE, 0); nextExecutionTime.set(Calendar.SECOND, 0); nextExecutionTime.set(Calendar.MILLISECOND, 0); if (nextExecutionTime.before(now)) { nextExecutionTime.add(Calendar.WEEK_OF_YEAR, 1); } return nextExecutionTime; } }
在上述代碼中,我們使用ScheduledExecutorService
的scheduleAtFixedRate()
方法創(chuàng)建了一個(gè)定時(shí)任務(wù),該任務(wù)會(huì)在每周二晚上6點(diǎn)自動(dòng)生成一條devops流水線。initialDelay
參數(shù)表示當(dāng)前時(shí)間到下一個(gè)執(zhí)行時(shí)間的時(shí)間間隔,7 * 24 * 60 * 60 * 1000
參數(shù)表示每隔7天執(zhí)行一次。
要取消定時(shí)任務(wù),可以調(diào)用future.cancel(true)
方法。取消后,定時(shí)任務(wù)將不再執(zhí)行。
請(qǐng)注意,上述代碼中的取消和關(guān)閉操作是注釋掉的。如果要取消定時(shí)任務(wù),可以取消注釋future.cancel(true)
;如果要關(guān)閉ScheduledExecutorService
,可以取消注釋executor.shutdown()
。
到此這篇關(guān)于Java定時(shí)任務(wù)取消的示例代碼的文章就介紹到這了,更多相關(guān)java定時(shí)任務(wù)取消內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Java實(shí)現(xiàn)并發(fā)執(zhí)行定時(shí)任務(wù)并手動(dòng)控制開始結(jié)束
- Java定時(shí)任務(wù)Timer、TimerTask與ScheduledThreadPoolExecutor詳解
- Java?@Scheduled定時(shí)任務(wù)不執(zhí)行解決辦法
- Java實(shí)現(xiàn)定時(shí)任務(wù)的方法總結(jié)
- Java?Elastic-Job分布式定時(shí)任務(wù)使用方法介紹
- java實(shí)現(xiàn)周期性執(zhí)行(定時(shí)任務(wù))
- java8中定時(shí)任務(wù)最佳實(shí)現(xiàn)方式(實(shí)現(xiàn)原理)
相關(guān)文章
Java實(shí)現(xiàn)線性表的鏈?zhǔn)酱鎯?chǔ)
這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)線性表的鏈?zhǔn)酱鎯?chǔ),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-10-10詳解JDBC對(duì)Mysql utf8mb4字符集的處理
這篇文章主要介紹了詳解JDBC對(duì)Mysql utf8mb4字符集的處理,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-11-11java之scan.next()與scan.nextline()函數(shù)的使用及區(qū)別
這篇文章主要介紹了java之scan.next()與scan.nextline()函數(shù)的使用及區(qū)別,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-04-04Java集合Iterator迭代的實(shí)現(xiàn)方法
這篇文章主要介紹了Java集合Iterator迭代接口的實(shí)現(xiàn)方法,非常不錯(cuò),具有參考借鑒家,對(duì)Java 結(jié)合iterator知識(shí)感興趣的朋友一起看看吧2016-08-08spring-boot-plus V1.4.0發(fā)布 集成用戶角色權(quán)限部門管理(推薦)
這篇文章主要介紹了spring-boot-plus V1.4.0發(fā)布 集成用戶角色權(quán)限部門管理,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值需要的朋友可以參考下2019-11-11Java基于二維數(shù)組實(shí)現(xiàn)的數(shù)獨(dú)問題示例
這篇文章主要介紹了Java基于二維數(shù)組實(shí)現(xiàn)的數(shù)獨(dú)問題,涉及java針對(duì)數(shù)組的遍歷、計(jì)算、轉(zhuǎn)換等相關(guān)操作技巧,需要的朋友可以參考下2018-01-01