更新時間:2022-03-30 09:46:01 來源:動力節(jié)點 瀏覽1424次
下面給出的是Java Timer定時器類支持的方法。
void cancel():該方法終止當前或本次Timer,同時取消所有當前調(diào)度的任務。
int purge():取消后,purge() 方法從隊列中移除所有取消的任務。
void schedule(TimerTask task, Date time):將要在指定時間執(zhí)行的任務排成一行。
void schedule(TimerTask task, Date firstTime, long period):也將任務排成指定開始時間,然后重復執(zhí)行。
void schedule(TimerTask task, long delay):延遲后也會排隊執(zhí)行任務。
void schedule(TimerTask task, long delay, long period):它還排列任務以重復執(zhí)行,但它以指定的延遲開始。
void scheduleAtFixedRate(TimerTask task, Date firstTime, long period): 也將任務排成一行,以重復固定速率執(zhí)行,任務在指定時間開始。
void scheduleAtFixedRate(TimerTask task, long delay, long period):它還排列任務以重復但以固定速率執(zhí)行,并且任務以指定的延遲開始。
下面是一個 Java Timer 示例,其中包括調(diào)度指定任務以以固定延遲重復執(zhí)行的功能,并且該任務具有一些指定的開始時間。
首先,我們聲明了一個擴展 TimerTask 類的 Helper 類。在這個 TimerTask 中,我們初始化了一個變量,用于檢查執(zhí)行的計數(shù)。
TimerTask 類的 run() 方法用于打印執(zhí)行完成的次數(shù)。在 main 方法中,我們使用了 schedule() 方法的“void schedule(TimerTask task, Date firstTime, long period)”變體來執(zhí)行 run() 方法任意多次。
我們明確需要停止執(zhí)行,否則 run() 方法將繼續(xù)執(zhí)行。
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 并丟棄任何計劃任務,但它不會干擾任何當前正在執(zhí)行的任務或操作。
在這個例子中,我們將看到 for 循環(huán)中的語句將繼續(xù)執(zhí)行,即使在遇到第一個“停止調(diào)用”語句后,即“i”等于 3。
現(xiàn)在我們將繼續(xù)下面給出的 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”變?yōu)?3 時立即退出循環(huán)。
現(xiàn)在我們已經(jīng)退出循環(huán),我們嘗試返回從隊列中刪除的任務數(shù)。為此,我們在引用變量的幫助下簡單地調(diào)用了 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定時器實現(xiàn)已經(jīng)有所了解,大家如果想了解更多相關(guān)知識,可以關(guān)注一下java timer的用法,這對大家的學習一定會有所幫助的。