更新時間:2022-03-30 09:46:01 來源:動力節點 瀏覽1345次
下面給出的是Java Timer定時器類支持的方法。
void cancel():該方法終止當前或本次Timer,同時取消所有當前調度的任務。
int purge():取消后,purge() 方法從隊列中移除所有取消的任務。
void schedule(TimerTask task, Date time):將要在指定時間執行的任務排成一行。
void schedule(TimerTask task, Date firstTime, long period):也將任務排成指定開始時間,然后重復執行。
void schedule(TimerTask task, long delay):延遲后也會排隊執行任務。
void schedule(TimerTask task, long delay, long period):它還排列任務以重復執行,但它以指定的延遲開始。
void scheduleAtFixedRate(TimerTask task, Date firstTime, long period): 也將任務排成一行,以重復固定速率執行,任務在指定時間開始。
void scheduleAtFixedRate(TimerTask task, long delay, long period):它還排列任務以重復但以固定速率執行,并且任務以指定的延遲開始。
下面是一個 Java Timer 示例,其中包括調度指定任務以以固定延遲重復執行的功能,并且該任務具有一些指定的開始時間。
首先,我們聲明了一個擴展 TimerTask 類的 Helper 類。在這個 TimerTask 中,我們初始化了一個變量,用于檢查執行的計數。
TimerTask 類的 run() 方法用于打印執行完成的次數。在 main 方法中,我們使用了 schedule() 方法的“void schedule(TimerTask task, Date firstTime, long period)”變體來執行 run() 方法任意多次。
我們明確需要停止執行,否則 run() 方法將繼續執行。
import java.util.Timer;
import java.util.TimerTask;
class Helper extends TimerTask {
public static int i = 1;
// TimerTask.run() method will be used to perform the action of the task
public void run() {
System.out.println("This is called " + i++ + " time");
}
}
public class example {
public static void main(String[] args) {
Timer timer = new Timer();
// Helper class extends TimerTask
TimerTask task = new Helper();
/*
* Schedule() method calls for timer class.
* void schedule(TimerTask task, Date firstTime, long period)
*/
timer.schedule(task, 200, 5000);
}
}
輸出:
這是包含 cancel() 方法功能的 Java Timer 類的示例。眾所周知,cancel() 方法用于終止此 Timer 并丟棄任何計劃任務,但它不會干擾任何當前正在執行的任務或操作。
在這個例子中,我們將看到 for 循環中的語句將繼續執行,即使在遇到第一個“停止調用”語句后,即“i”等于 3。
現在我們將繼續下面給出的 purge() 方法的示例。
import java.util.*;
public class example {
public static void main(String[] args) {
Timer timer = new Timer();
TimerTask task = new TimerTask() {
// run() method to carry out the action of the task
public void run() {
for(int i=1; i<= 10; i++) {
System.out.println("Keep on calling");
if(i >= 3) {
System.out.println("Stop calling");
// cancel method to cancel the execution
timer.cancel();
}
}
};
};
/*
* schedule() method to schedule the execution with start time
*/
timer.schedule(task, 5000, 5000);
}
}
輸出:
如果您比較為 cancel() 和 purge() 方法給出的示例,您會注意到在下面的 purge() 方法示例中,在 cancel() 方法之后放置了一個 break 語句。這將允許控件在“i”變為 3 時立即退出循環。
現在我們已經退出循環,我們嘗試返回從隊列中刪除的任務數。為此,我們在引用變量的幫助下簡單地調用了 purge 方法。
import java.util.*;
public class example {
public static void main(String[] args) {
Timer timer = new Timer();
TimerTask task = new TimerTask() {
// run() method to carry out the action of the task
public void run() {
for(int i=1; i<= 10; i++) {
System.out.println("Keep on calling");
if(i >= 3) {
System.out.println("Stop calling");
// cancel method to cancel the execution
timer.cancel();
break;
}
}
// Purge after cancellation
System.out.println("Purge " + timer.purge());
};
};
/*
* schedule() method to schedule the execution with start time
*/
timer.schedule(task, 5000, 5000);
}
}
輸出:
通過上述介紹,相信大家對Java定時器實現已經有所了解,大家如果想了解更多相關知識,可以關注一下java timer的用法,這對大家的學習一定會有所幫助的。
0基礎 0學費 15天面授
有基礎 直達就業
業余時間 高薪轉行
工作1~3年,加薪神器
工作3~5年,晉升架構
提交申請后,顧問老師會電話與您溝通安排學習