Java定時任務(wù)取消的示例代碼
java定時任務(wù)如何取消,并比如,我之前想每周二晚上6點自動生成一條devops流水線,現(xiàn)在我想停掉
答案: 在Java中,可以使用ScheduledExecutorService類來創(chuàng)建定時任務(wù)。要取消定時任務(wù),可以調(diào)用ScheduledFuture對象的cancel()方法。
以下是一個示例代碼,演示如何創(chuàng)建一個每周二晚上6點自動生成一條devops流水線的定時任務(wù),并在需要時取消它:
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)建一個ScheduledExecutorService對象
ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);
// 獲取當前時間
Calendar now = Calendar.getInstance();
// 計算下一個周二晚上6點的時間
Calendar nextExecutionTime = getNextExecutionTime(now);
// 計算當前時間到下一個執(zhí)行時間的時間間隔
long initialDelay = nextExecutionTime.getTimeInMillis() - now.getTimeInMillis();
// 創(chuàng)建定時任務(wù)
ScheduledFuture<?> future = executor.scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
// 自動生成devops流水線的代碼
System.out.println("Generating devops pipeline...");
}
}, initialDelay, 7 * 24 * 60 * 60 * 1000, TimeUnit.MILLISECONDS);
// 在需要時取消定時任務(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)建了一個定時任務(wù),該任務(wù)會在每周二晚上6點自動生成一條devops流水線。initialDelay參數(shù)表示當前時間到下一個執(zhí)行時間的時間間隔,7 * 24 * 60 * 60 * 1000參數(shù)表示每隔7天執(zhí)行一次。
要取消定時任務(wù),可以調(diào)用future.cancel(true)方法。取消后,定時任務(wù)將不再執(zhí)行。
請注意,上述代碼中的取消和關(guān)閉操作是注釋掉的。如果要取消定時任務(wù),可以取消注釋future.cancel(true);如果要關(guān)閉ScheduledExecutorService,可以取消注釋executor.shutdown()。
到此這篇關(guān)于Java定時任務(wù)取消的示例代碼的文章就介紹到這了,更多相關(guān)java定時任務(wù)取消內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot分布式WebSocket的實現(xiàn)指南
在現(xiàn)代Web應(yīng)用中,實時通信已成為基本需求,而WebSocket是實現(xiàn)這一功能的核心技術(shù),本文將詳細介紹如何在Spring Boot項目中實現(xiàn)分布式WebSocket,包括完整的技術(shù)方案、實現(xiàn)步驟和核心代碼,需要的朋友可以參考下2025-10-10
Java位集合之BitMap實現(xiàn)和應(yīng)用詳解
這篇文章主要介紹了Java位集合之BitMap實現(xiàn)和應(yīng)用的相關(guān)資料,BitMap是一種高效的數(shù)據(jù)結(jié)構(gòu),適用于快速排序、去重和查找等操作,通過簡單的數(shù)組和位運算,可以在Java中實現(xiàn)BitMap,從而節(jié)省存儲空間并提高性能,需要的朋友可以參考下2024-12-12
spring boot如何使用spring AOP實現(xiàn)攔截器
本篇文章主要介紹了spring boot如何使用spring AOP實現(xiàn)攔截器,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-04-04
java中PriorityBlockingQueue的入隊知識點總結(jié)
在本篇文章里小編給大家整理一篇關(guān)于java中PriorityBlockingQueue的入隊知識點總結(jié)內(nèi)容,有需要的朋友們可以學習下。2021-01-01
MyBatis中的@SelectProvider注解源碼分析
這篇文章主要介紹了MyBatis中的@SelectProvider注解源碼分析,@SelectProvider功能就是用來單獨寫一個class類與方法,用來提供一些xml或者注解中不好寫的sql,今天就來說下這個注解的具體用法與源碼,需要的朋友可以參考下2024-01-01

