日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問(wèn)題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷(xiāo)解決方案
Timer和TimerTask詳解

以下內(nèi)容根據(jù) The JavaTM Tutorial 和相關(guān)API doc翻譯整理,以供日后參考:

1.概覽

Timer是一種定時(shí)器工具,用來(lái)在一個(gè)后臺(tái)線程計(jì)劃執(zhí)行指定任務(wù)。它可以計(jì)劃執(zhí)行一個(gè)任務(wù)一次或反復(fù)多次。

TimerTask一個(gè)抽象類(lèi),它的子類(lèi)代表一個(gè)可以被Timer計(jì)劃的任務(wù)。

簡(jiǎn)單的一個(gè)例程:

 
 
 
  1. import java.util.Timer;
  2. import java.util.TimerTask;
  3. /**
  4. * Simple demo that uses java.util.Timer to schedule a task to execute
  5. * once 5 seconds have passed.
  6. */
  7. public class Reminder {
  8.     Timer timer;
  9.     public Reminder(int seconds) {
  10.         timer = new Timer();
  11.         timer.schedule(new RemindTask(), seconds*1000);
  12.     }
  13.     class RemindTask extends TimerTask {
  14.         public void run() {
  15.             System.out.println("Time's up!");
  16.             timer.cancel(); //Terminate the timer thread
  17.         }
  18.     }
  19.     public static void main(String args[]) {
  20.         System.out.println("About to schedule task.");
  21.         new Reminder(5);
  22.         System.out.println("Task scheduled.");
  23.     }
  24. }

運(yùn)行這個(gè)小例子,你會(huì)首先看到:

About to schedule task.

5秒鐘之后你會(huì)看到:

Time's up!

這個(gè)小例子可以說(shuō)明一些用Timer線程實(shí)現(xiàn)和計(jì)劃執(zhí)行一個(gè)任務(wù)的基礎(chǔ)步驟:

  •  實(shí)現(xiàn)自定義的TimerTask的子類(lèi),run方法包含要執(zhí)行的任務(wù)代碼,在這個(gè)例子里,這個(gè)子類(lèi)就是RemindTask。
  • 實(shí)例化Timer類(lèi),創(chuàng)建計(jì)時(shí)器后臺(tái)線程。
  • 實(shí)例化任務(wù)對(duì)象 (new RemindTask()).
  • 制定執(zhí)行計(jì)劃。這里用schedule方法,***個(gè)參數(shù)是TimerTask對(duì)象,第二個(gè)參數(shù)表示開(kāi)始執(zhí)行前的延時(shí)時(shí)間(單位是milliseconds,這里定義了5000)。還有一種方法可以指定任務(wù)的執(zhí)行時(shí)間,如下例,指定任務(wù)在11:01 p.m.執(zhí)行:
 
 
 
  1. //Get the Date corresponding to 11:01:00 pm today.
  2. Calendar calendar = Calendar.getInstance();
  3. calendar.set(Calendar.HOUR_OF_DAY, 23);
  4. calendar.set(Calendar.MINUTE, 1);
  5. calendar.set(Calendar.SECOND, 0);
  6. Date time = calendar.getTime();
  7. timer = new Timer();
  8. timer.schedule(new RemindTask(), time);

2.終止Timer線程

默認(rèn)情況下,只要一個(gè)程序的timer線程在運(yùn)行,那么這個(gè)程序就會(huì)保持運(yùn)行。當(dāng)然,你可以通過(guò)以下四種方法終止一個(gè)timer線程:

調(diào)用timer的cancle方法。你可以從程序的任何地方調(diào)用此方法,甚至在一個(gè)timer task的run方法里。

讓timer線程成為一個(gè)daemon線程(可以在創(chuàng)建timer時(shí)使用new Timer(true)達(dá)到這個(gè)目地),這樣當(dāng)程序只有daemon線程的時(shí)候,它就會(huì)自動(dòng)終止運(yùn)行。

當(dāng)timer相關(guān)的所有task執(zhí)行完畢以后,刪除所有此timer對(duì)象的引用(置成null),這樣timer線程也會(huì)終止。

調(diào)用System.exit方法,使整個(gè)程序(所有線程)終止。

Reminder的例子使用了***種方式。在這里不能使用第二種方式,因?yàn)檫@里需要程序保持運(yùn)行直到timer的任務(wù)執(zhí)行完成,如果設(shè)成daemon,那么當(dāng)main線程結(jié)束的時(shí)候,程序只剩下timer這個(gè)daemon線程,于是程序不會(huì)等timer線程執(zhí)行task就終止了。

有些時(shí)候,程序的終止與否并不只與timer線程有關(guān)。舉個(gè)例子,如果我們使用AWT來(lái)beep,那么AWT會(huì)自動(dòng)創(chuàng)建一個(gè)非daemon線程來(lái)保持程序的運(yùn)行。

 
 
 
  1. import java.util.Timer;
  2. import java.util.TimerTask;
  3. import java.awt.Toolkit;
  4. /**
  5. * Simple demo that uses java.util.Timer to schedule a task to execute
  6. * once 5 seconds have passed.
  7. */
  8. public class ReminderBeep {
  9.     Toolkit toolkit;
  10.     Timer timer;
  11.     public ReminderBeep(int seconds) {
  12.         toolkit = Toolkit.getDefaultToolkit();
  13.         timer = new Timer();
  14.         timer.schedule(new RemindTask(), seconds*1000);
  15.     }
  16.     class RemindTask extends TimerTask {
  17.         public void run() {
  18.             System.out.println("Time's up!");
  19.     toolkit.beep();
  20.     //timer.cancel(); //Not necessary because we call System.exit
  21.     System.exit(0);   //Stops the AWT thread (and everything else)
  22.         }
  23.     }
  24.     public static void main(String args[]) {
  25. System.out.println("About to schedule task.");
  26.         new ReminderBeep(5);
  27. System.out.println("Task scheduled.");
  28.     }
  29. }

3.反復(fù)執(zhí)行一個(gè)任務(wù)

先看一個(gè)例子:

 
 
 
  1. public class AnnoyingBeep {
  2.     Toolkit toolkit;
  3.     Timer timer;
  4.     public AnnoyingBeep() {
  5.         toolkit = Toolkit.getDefaultToolkit();
  6.         timer = new Timer();
  7.         timer.schedule(new RemindTask(),
  8.                0,        //initial delay
  9.                1*1000);  //subsequent rate
  10.     }
  11.     class RemindTask extends TimerTask {
  12.         int numWarningBeeps = 3;
  13.         public void run() {
  14.             if (numWarningBeeps > 0) {
  15.                 toolkit.beep();
  16.                 System.out.println("Beep!");
  17.                 numWarningBeeps--;
  18.             } else {
  19.                 toolkit.beep(); 
  20.                 System.out.println("Time's up!");
  21.                 //timer.cancel(); //Not necessary because we call System.exit
  22.                 System.exit(0);   //Stops the AWT thread (and everything else)
  23.             }
  24.         }
  25.     }
  26.     ...

執(zhí)行,你會(huì)看到如下輸出:

 
 
 
  1. Task scheduled.
  2. Beep! 
  3. Beep! //one second after the first beep
  4. Beep! //one second after the second beep
  5. Time's up! //one second after the third beep

這里使用了三個(gè)參數(shù)的schedule方法用來(lái)指定task每隔一秒執(zhí)行一次。如下所列為所有Timer類(lèi)用來(lái)制定計(jì)劃反復(fù)執(zhí)行task的方法 :

  •  schedule(TimerTask task, long delay, long period)
  • schedule(TimerTask task, Date time, long period)
  • scheduleAtFixedRate(TimerTask task, long delay, long period)
  • scheduleAtFixedRate(TimerTask task, Date firstTime, long period)

當(dāng)計(jì)劃反復(fù)執(zhí)行的任務(wù)時(shí),如果你注重任務(wù)執(zhí)行的平滑度,那么請(qǐng)使用schedule方法,如果你在乎的是任務(wù)的執(zhí)行頻度那么使用scheduleAtFixedRate方法。 例如,這里使用了schedule方法,這就意味著所有beep之間的時(shí)間間隔至少為1秒,也就是說(shuō),如果有一個(gè)beap因?yàn)槟撤N原因遲到了(未按計(jì)劃執(zhí)行),那么余下的所有beep都要延時(shí)執(zhí)行。如果我們想讓這個(gè)程序正好在3秒以后終止,無(wú)論哪一個(gè)beep因?yàn)槭裁丛虮谎訒r(shí),那么我們需要使用scheduleAtFixedRate方法,這樣當(dāng)***個(gè)beep遲到時(shí),那么后面的beep就會(huì)以最快的速度緊密執(zhí)行(***限度的壓縮間隔時(shí)間)。

4.進(jìn)一步分析schedule和scheduleAtFixedRate

(1)2個(gè)參數(shù)的schedule在制定任務(wù)計(jì)劃時(shí), 如果指定的計(jì)劃執(zhí)行時(shí)間scheduledExecutionTime<=systemCurrentTime,則task會(huì)被立即執(zhí)行。scheduledExecutionTime不會(huì)因?yàn)槟骋粋€(gè)task的過(guò)度執(zhí)行而改變。

(2)3個(gè)參數(shù)的schedule在制定反復(fù)執(zhí)行一個(gè)task的計(jì)劃時(shí),每一次執(zhí)行這個(gè)task的計(jì)劃執(zhí)行時(shí)間隨著前一次的實(shí)際執(zhí)行時(shí)間而變,也就是scheduledExecutionTime(第n+1次)=realExecutionTime(第n次)+periodTime。也就是說(shuō)如果第n次執(zhí)行task時(shí),由于某種原因這次執(zhí)行時(shí)間過(guò)長(zhǎng),執(zhí)行完后的systemCurrentTime>=scheduledExecutionTime(第n+1次),則此時(shí)不做時(shí)隔等待,立即執(zhí)行第n+1次task,而接下來(lái)的第n+2次task的scheduledExecutionTime(第n+2次)就隨著變成了realExecutionTime(第n+1次)+periodTime。說(shuō)白了,這個(gè)方法更注重保持間隔時(shí)間的穩(wěn)定。

(3)3個(gè)參數(shù)的scheduleAtFixedRate在制定反復(fù)執(zhí)行一個(gè)task的計(jì)劃時(shí),每一次執(zhí)行這個(gè)task的計(jì)劃執(zhí)行時(shí)間在最初就被定下來(lái)了,也就是scheduledExecutionTime(第n次)=firstExecuteTime+n*periodTime;如果第n次執(zhí)行task時(shí),由于某種原因這次執(zhí)行時(shí)間過(guò)長(zhǎng),執(zhí)行完后的systemCurrentTime>=scheduledExecutionTime(第n+1次),則此時(shí)不做period間隔等待,立即執(zhí)行第n+1次task,而接下來(lái)的第n+2次的task的scheduledExecutionTime(第n+2次)依然還是firstExecuteTime+(n+2)*periodTime這在***次執(zhí)行task就定下來(lái)了。說(shuō)白了,這個(gè)方法更注重保持執(zhí)行頻率的穩(wěn)定。

5.一些注意的問(wèn)題

  • 每一個(gè)Timer僅對(duì)應(yīng)唯一一個(gè)線程。
  • Timer不保證任務(wù)執(zhí)行的十分精確。
  • Timer類(lèi)的線程安全的。

網(wǎng)站題目:Timer和TimerTask詳解
網(wǎng)站URL:http://m.5511xx.com/article/ccsgepc.html