jdk中的定時器,jdk定時器

來源:互聯網
上載者:User

jdk中的定時器,jdk定時器

首先看一下jdk內建定時器:

  一種工具,線程用其安排以後在後台線程中執行的任務。可安排任務執行一次,或者定期重複執行。與每個 Timer 對象相對應的是單個後台線程,用於順序地執行所有計時器任務。計時器任務應該迅速完成。如果完成某個計時器任務的時間太長,那麼它會“獨佔”計時器的任務執行線程。因此,這就可能延遲後續任務的執行,而這些任務就可能“堆在一起”,並且在上述不友好的任務最終完成時才能夠被快速連續地執行。

schedule(TimerTask task,long delay) 安排在指定延遲後執行指定的任務。
schedule(TimerTask task,Date time) 安排在指定的時間執行指定的任務。如果此時間已過去,則安排立即執行該任務。
schedule(TimerTask task, long delay, long period) 安排指定的任務從指定的延遲後開始進行重複的固定順延強制。如果由於任何原因(如記憶體回收或其他後台活動)而延遲了某次執行,則後續執行也將被延遲
schedule(TimerTask task,Date firstTime,long period) 安排指定的任務在指定的時間開始進行重複的固定順延強制。如果由於任何原因(如記憶體回收或其他後台活動)而延遲了某次執行,則後續執行也將被延遲。

 1 package test; 2  3 import java.text.ParseException; 4 import java.text.SimpleDateFormat; 5 import java.util.Date; 6 import java.util.Timer; 7 import java.util.TimerTask; 8  9 /**10  * jdk內建定時器11  * 12  * @author LIUTIE13  *14  */15 public class JDKTimer {16     17 18     public static void main(String[] args) throws ParseException {19         //日期格式工具20         final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");21         22         Timer timer = new Timer();23         // 10s後執行定時器,僅執行一次24         System.out.print(sdf.format(new Date()));25         System.out.println("the timer one will be executed after 10 seconds...");26         long milliseconds = 10 * 1000;27         timer.schedule(new TimerTask() {28 29             @Override30             public void run() {31                 System.out.print(sdf.format(new Date()));32                 System.out.println("the timer one has finished execution");33             }34         }, milliseconds);35         36         //12秒後執行定時器,每1s執行一次37         System.out.print(sdf.format(new Date()));38         System.out.println("the timer two will be executed after 12 seconds...");39         //啟動後延遲時間40         long afterSs = 12 * 1000;41         //執行循環42         long intervalSs1 = 1 * 1000;43         timer.schedule(new TimerTask() {44             // 執行計數器45             int i = 0;46 47             @Override48             public void run() {49                 System.out.print(sdf.format(new Date()));50                 System.out.println("the timer two has execution " + (++i) + " timers");51                 // 執行10次後關閉定時器52                 if (i == 10) {53                     this.cancel();54                 }55             }56         }, afterSs, intervalSs1);57         58         59         // 指定時間執行定時器,僅執行一次60         System.out.print(sdf.format(new Date()));61         System.out.println("the timer three will be executed at 2017-06-27 21:47:00...");62         Date date = sdf.parse("2017-06-27 21:47:00");63         timer.schedule(new TimerTask() {64 65             @Override66             public void run() {67                 System.out.print(sdf.format(new Date()));68                 System.out.println("the timer three has finished execution");69             }70         }, date);71         72         // 從指定時間開始周期性執行73         System.out.print(sdf.format(new Date()));74         System.out.println("the timer four will be executed at 2017-06-27 21:48:00...");75         // 執行間隔周期76         long intervalSs = 1 * 1000;77         // 開始執行時間78         Date beginTime = sdf.parse("2017-06-27 21:48:00");79         timer.schedule(new TimerTask() {80             // 執行計數器81             int i = 0;82 83             @Override84             public void run() {85                 System.out.print(sdf.format(new Date()));86                 System.out.println("the timer four has execution " + (++i) + " timers");87                 // 執行10次後關閉定時器88                 if (i == 10) {89                     this.cancel();90                 }91             }92         }, beginTime, intervalSs);93     }94 95 }
View Code

執行結果

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 execution2017-06-27 21:46:36the timer two has execution 1 timers2017-06-27 21:46:37the timer two has execution 2 timers2017-06-27 21:46:38the timer two has execution 3 timers2017-06-27 21:46:39the timer two has execution 4 timers2017-06-27 21:46:40the timer two has execution 5 timers2017-06-27 21:46:41the timer two has execution 6 timers2017-06-27 21:46:42the timer two has execution 7 timers2017-06-27 21:46:43the timer two has execution 8 timers2017-06-27 21:46:44the timer two has execution 9 timers2017-06-27 21:46:45the timer two has execution 10 timers2017-06-27 21:47:00the timer three has finished execution2017-06-27 21:48:00the timer four has execution 1 timers2017-06-27 21:48:01the timer four has execution 2 timers2017-06-27 21:48:02the timer four has execution 3 timers2017-06-27 21:48:03the timer four has execution 4 timers2017-06-27 21:48:04the timer four has execution 5 timers2017-06-27 21:48:05the timer four has execution 6 timers2017-06-27 21:48:06the timer four has execution 7 timers2017-06-27 21:48:07the timer four has execution 8 timers2017-06-27 21:48:08the timer four has execution 9 timers2017-06-27 21:48:09the timer four has execution 10 timers
View Code

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.