本篇文章給大家分享的是有關如何正確的使用jdk定時器,小編覺得挺實用的,因此分享給大家學習,希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。
創(chuàng)新互聯(lián)堅持“要么做到,要么別承諾”的工作理念,服務領域包括:成都做網(wǎng)站、成都網(wǎng)站設計、企業(yè)官網(wǎng)、英文網(wǎng)站、手機端網(wǎng)站、網(wǎng)站推廣等服務,滿足客戶于互聯(lián)網(wǎng)時代的鹿城網(wǎng)站設計、移動媒體設計的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡建設合作伙伴!
首先看一下jdk自帶定時器:
一種工具,線程用其安排以后在后臺線程中執(zhí)行的任務??砂才湃蝿請?zhí)行一次,或者定期重復執(zhí)行。與每個 Timer 對象相對應的是單個后臺線程,用于順序地執(zhí)行所有計時器任務。計時器任務應該迅速完成。如果完成某個計時器任務的時間太長,那么它會“獨占”計時器的任務執(zhí)行線程。因此,這就可能延遲后續(xù)任務的執(zhí)行,而這些任務就可能“堆在一起”,并且在上述不友好的任務最終完成時才能夠被快速連續(xù)地執(zhí)行。
schedule(TimerTask task,long delay) 安排在指定延遲后執(zhí)行指定的任務。
schedule(TimerTask task,Date time) 安排在指定的時間執(zhí)行指定的任務。如果此時間已過去,則安排立即執(zhí)行該任務。
schedule(TimerTask task, long delay, long period) 安排指定的任務從指定的延遲后開始進行重復的固定延遲執(zhí)行。如果由于任何原因(如垃圾回收或其他后臺活動)而延遲了某次執(zhí)行,則后續(xù)執(zhí)行也將被延遲
schedule(TimerTask task,Date firstTime,long period) 安排指定的任務在指定的時間開始進行重復的固定延遲執(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次后關閉定時器 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次后關閉定時器 if (i == 10) { this.cancel(); } } }, beginTime, intervalSs); } }
執(zhí)行結果
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ぷ鲿姷交蛴玫降摹OM隳芡ㄟ^這篇文章學到更多知識。更多詳情敬請關注創(chuàng)新互聯(lián)行業(yè)資訊頻道。