您好,登錄后才能下訂單哦!
本篇文章給大家分享的是有關(guān)如何正確的使用jdk定時器,小編覺得挺實用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。
首先看一下jdk自帶定時器:
一種工具,線程用其安排以后在后臺線程中執(zhí)行的任務(wù)??砂才湃蝿?wù)執(zhí)行一次,或者定期重復(fù)執(zhí)行。與每個 Timer 對象相對應(yīng)的是單個后臺線程,用于順序地執(zhí)行所有計時器任務(wù)。計時器任務(wù)應(yīng)該迅速完成。如果完成某個計時器任務(wù)的時間太長,那么它會“獨占”計時器的任務(wù)執(zhí)行線程。因此,這就可能延遲后續(xù)任務(wù)的執(zhí)行,而這些任務(wù)就可能“堆在一起”,并且在上述不友好的任務(wù)最終完成時才能夠被快速連續(xù)地執(zhí)行。
schedule(TimerTask task,long delay) 安排在指定延遲后執(zhí)行指定的任務(wù)。
schedule(TimerTask task,Date time) 安排在指定的時間執(zhí)行指定的任務(wù)。如果此時間已過去,則安排立即執(zhí)行該任務(wù)。
schedule(TimerTask task, long delay, long period) 安排指定的任務(wù)從指定的延遲后開始進(jìn)行重復(fù)的固定延遲執(zhí)行。如果由于任何原因(如垃圾回收或其他后臺活動)而延遲了某次執(zhí)行,則后續(xù)執(zhí)行也將被延遲
schedule(TimerTask task,Date firstTime,long period) 安排指定的任務(wù)在指定的時間開始進(jìn)行重復(fù)的固定延遲執(zhí)行。如果由于任何原因(如垃圾回收或其他后臺活動)而延遲了某次執(zhí)行,則后續(xù)執(zhí)行也將被延遲。
package test; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Timer; import java.util.TimerTask; /** * jdk自帶定時器 * * @author LIUTIE * */ public class JDKTimer { public static void main(String[] args) throws ParseException { //日期格式工具 final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Timer timer = new Timer(); // 10s后執(zhí)行定時器,僅執(zhí)行一次 System.out.print(sdf.format(new Date())); System.out.println("the timer one will be executed after 10 seconds..."); long milliseconds = 10 * 1000; timer.schedule(new TimerTask() { @Override public void run() { System.out.print(sdf.format(new Date())); System.out.println("the timer one has finished execution"); } }, milliseconds); //12秒后執(zhí)行定時器,每1s執(zhí)行一次 System.out.print(sdf.format(new Date())); System.out.println("the timer two will be executed after 12 seconds..."); //啟動后延遲時間 long afterSs = 12 * 1000; //執(zhí)行周期 long intervalSs1 = 1 * 1000; timer.schedule(new TimerTask() { // 執(zhí)行計數(shù)器 int i = 0; @Override public void run() { System.out.print(sdf.format(new Date())); System.out.println("the timer two has execution " + (++i) + " timers"); // 執(zhí)行10次后關(guān)閉定時器 if (i == 10) { this.cancel(); } } }, afterSs, intervalSs1); // 指定時間執(zhí)行定時器,僅執(zhí)行一次 System.out.print(sdf.format(new Date())); System.out.println("the timer three will be executed at 2017-06-27 21:47:00..."); Date date = sdf.parse("2017-06-27 21:47:00"); timer.schedule(new TimerTask() { @Override public void run() { System.out.print(sdf.format(new Date())); System.out.println("the timer three has finished execution"); } }, date); // 從指定時間開始周期性執(zhí)行 System.out.print(sdf.format(new Date())); System.out.println("the timer four will be executed at 2017-06-27 21:48:00..."); // 執(zhí)行間隔周期 long intervalSs = 1 * 1000; // 開始執(zhí)行時間 Date beginTime = sdf.parse("2017-06-27 21:48:00"); timer.schedule(new TimerTask() { // 執(zhí)行計數(shù)器 int i = 0; @Override public void run() { System.out.print(sdf.format(new Date())); System.out.println("the timer four has execution " + (++i) + " timers"); // 執(zhí)行10次后關(guān)閉定時器 if (i == 10) { this.cancel(); } } }, beginTime, intervalSs); } }
執(zhí)行結(jié)果
2017-06-27 21:46:24the timer one will be executed after 10 seconds... 2017-06-27 21:46:24the timer two will be executed after 12 seconds... 2017-06-27 21:46:24the timer three will be executed at 2017-06-27 21:47:00... 2017-06-27 21:46:24the timer four will be executed at 2017-06-27 21:48:00... 2017-06-27 21:46:34the timer one has finished execution 2017-06-27 21:46:36the timer two has execution 1 timers 2017-06-27 21:46:37the timer two has execution 2 timers 2017-06-27 21:46:38the timer two has execution 3 timers 2017-06-27 21:46:39the timer two has execution 4 timers 2017-06-27 21:46:40the timer two has execution 5 timers 2017-06-27 21:46:41the timer two has execution 6 timers 2017-06-27 21:46:42the timer two has execution 7 timers 2017-06-27 21:46:43the timer two has execution 8 timers 2017-06-27 21:46:44the timer two has execution 9 timers 2017-06-27 21:46:45the timer two has execution 10 timers 2017-06-27 21:47:00the timer three has finished execution 2017-06-27 21:48:00the timer four has execution 1 timers 2017-06-27 21:48:01the timer four has execution 2 timers 2017-06-27 21:48:02the timer four has execution 3 timers 2017-06-27 21:48:03the timer four has execution 4 timers 2017-06-27 21:48:04the timer four has execution 5 timers 2017-06-27 21:48:05the timer four has execution 6 timers 2017-06-27 21:48:06the timer four has execution 7 timers 2017-06-27 21:48:07the timer four has execution 8 timers 2017-06-27 21:48:08the timer four has execution 9 timers 2017-06-27 21:48:09the timer four has execution 10 timers
以上就是如何正確的使用jdk定時器,小編相信有部分知識點可能是我們?nèi)粘9ぷ鲿姷交蛴玫降?。希望你能通過這篇文章學(xué)到更多知識。更多詳情敬請關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。