更新時間:2022-08-09 11:33:59 來源:動力節(jié)點 瀏覽1803次
動力節(jié)點小編通過編程示例來向大家解釋如何使用 Java Timer 類在 Java 中設置計時器:
下面給出的是 Java Timer 類支持的方法。
void cancel():該方法終止當前或本次Timer,同時取消所有當前調度的任務。
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 示例,其中包括調度指定任務以以固定延遲重復執(zhí)行的功能,并且該任務具有一些指定的開始時間。
首先,我們聲明了一個擴展 TimerTask 類的 Helper 類。在這個 TimerTask 中,我們初始化了一個變量,用于檢查執(zhí)行的計數。
TimerTask 類的 run() 方法用于打印執(zhí)行完成的次數。在 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í)行,即使在遇到第一個“停止調用”語句后,即“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)在我們已經退出循環(huán),我們嘗試返回從隊列中刪除的任務數。為此,我們在引用變量的幫助下簡單地調用了 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計時器的示例”介紹,大家如果想了解更多相關知識,可以關注一下動力節(jié)點的Java在線學習,里面的課程內容從入門到精通,細致全面,很適合小白學習,希望對大家能夠有所幫助。