cancle() 是 TimerTask 類的方法。此方法用於在定時任務計劃執行 1 次或尚未運行時停止該定時任務。如果任務已安排重複執行,它將永遠不會再次運行。
用法
public boolean cancel()
返回
如果任務被安排為 1 次執行並且尚未運行,則返回 true,如果任務被安排為 1 次執行並且已經運行,則返回 false,
例子1
import java.util.*;
public class JavaTimerTaskCancelExample1{
public static void main(String[] args) {
// creating timer task, timer
Timer t = new Timer();
TimerTask tt = new TimerTask() {
@Override
public void run() {
for(int i=1; i<=10;i++)
{
System.out.println("Task Timer is on ");
}
};
};
t.scheduleAtFixedRate(tt, new Date(), 1000);
System.out.println("Task Timer cancel:" + tt.cancel());
}
}
輸出:
Task Timer is on Task Timer is on Task Timer is on Task Timer is on Task Timer is on Task Timer is on Task Timer is on Task Timer is on Task Timer is on Task Timer is on Task Timer cancel:true
例子2
import java.util.*;
public class JavaTimerTaskCancelExample2{
public static void main(String[] args) {
// creating timer task, timer
Timer t = new Timer();
TimerTask tt = new TimerTask() {
@Override
public void run() {
System.out.println("Task Timer is on");
};
};
t.scheduleAtFixedRate(tt, new Date(), 1000);
System.out.println("Task Timer cancel:" + tt.cancel());
}
}
輸出:
Task Timer is on Task Timer cancel:true
相關用法
- Java TimerTask scheduledExecutionTime()用法及代碼示例
- Java TimerTask run()用法及代碼示例
- Java Timer scheduleAtFixedRate()用法及代碼示例
- Java Timer purge()用法及代碼示例
- Java Timer schedule()用法及代碼示例
- Java Timer cancel()用法及代碼示例
- Java TimeZone setDefault()用法及代碼示例
- Java TimeUnit values()用法及代碼示例
- Java TimeZone getDisplayName(boolean, int)用法及代碼示例
- Java TimeUnit toSeconds()用法及代碼示例
- Java TimeZone observesDaylightTime()用法及代碼示例
- Java TimeZone getDisplayName(Locale locale)用法及代碼示例
- Java TimeZone getDefault()用法及代碼示例
- Java TimeZone useDaylightTime()用法及代碼示例
- Java SQL Timestamp before()用法及代碼示例
- Java TimeUnit toNanos()用法及代碼示例
- Java TimeZone hasSameRules()用法及代碼示例
- Java TimeUnit toDays()用法及代碼示例
- Java SQL Timestamp getNanos()用法及代碼示例
- Java TimeZone setRawOffset()用法及代碼示例
注:本文由純淨天空篩選整理自 Java TimerTask cancel() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。