jdk自帶定時(shí)器使用方法詳解
首先看一下jdk自帶定時(shí)器:
一種工具,線程用其安排以后在后臺(tái)線程中執(zhí)行的任務(wù)??砂才湃蝿?wù)執(zhí)行一次,或者定期重復(fù)執(zhí)行。與每個(gè) Timer 對(duì)象相對(duì)應(yīng)的是單個(gè)后臺(tái)線程,用于順序地執(zhí)行所有計(jì)時(shí)器任務(wù)。計(jì)時(shí)器任務(wù)應(yīng)該迅速完成。如果完成某個(gè)計(jì)時(shí)器任務(wù)的時(shí)間太長(zhǎng),那么它會(huì)“獨(dú)占”計(jì)時(shí)器的任務(wù)執(zhí)行線程。因此,這就可能延遲后續(xù)任務(wù)的執(zhí)行,而這些任務(wù)就可能“堆在一起”,并且在上述不友好的任務(wù)最終完成時(shí)才能夠被快速連續(xù)地執(zhí)行。
schedule(TimerTask task,long delay) 安排在指定延遲后執(zhí)行指定的任務(wù)。
schedule(TimerTask task,Date time) 安排在指定的時(shí)間執(zhí)行指定的任務(wù)。如果此時(shí)間已過去,則安排立即執(zhí)行該任務(wù)。
schedule(TimerTask task, long delay, long period) 安排指定的任務(wù)從指定的延遲后開始進(jìn)行重復(fù)的固定延遲執(zhí)行。如果由于任何原因(如垃圾回收或其他后臺(tái)活動(dòng))而延遲了某次執(zhí)行,則后續(xù)執(zhí)行也將被延遲
schedule(TimerTask task,Date firstTime,long period) 安排指定的任務(wù)在指定的時(shí)間開始進(jìn)行重復(fù)的固定延遲執(zhí)行。如果由于任何原因(如垃圾回收或其他后臺(tái)活動(dòng))而延遲了某次執(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自帶定時(shí)器 * * @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í)行定時(shí)器,僅執(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í)行定時(shí)器,每1s執(zhí)行一次 System.out.print(sdf.format(new Date())); System.out.println("the timer two will be executed after 12 seconds..."); //啟動(dòng)后延遲時(shí)間 long afterSs = 12 * 1000; //執(zhí)行周期 long intervalSs1 = 1 * 1000; timer.schedule(new TimerTask() { // 執(zhí)行計(jì)數(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)閉定時(shí)器 if (i == 10) { this.cancel(); } } }, afterSs, intervalSs1); // 指定時(shí)間執(zhí)行定時(shí)器,僅執(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); // 從指定時(shí)間開始周期性執(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í)行時(shí)間 Date beginTime = sdf.parse("2017-06-27 21:48:00"); timer.schedule(new TimerTask() { // 執(zhí)行計(jì)數(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)閉定時(shí)器 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
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- java當(dāng)中的定時(shí)器的4種使用方式
- JAVA中 Spring定時(shí)器的兩種實(shí)現(xiàn)方式
- java使用TimerTask定時(shí)器獲取指定網(wǎng)絡(luò)數(shù)據(jù)
- Java 定時(shí)器(Timer,TimerTask)詳解及實(shí)例代碼
- 解析Java中的定時(shí)器及使用定時(shí)器制作彈彈球游戲的示例
- Java 定時(shí)器(Timer)及線程池里使用定時(shí)器實(shí)例代碼
- java實(shí)現(xiàn)多線程之定時(shí)器任務(wù)
- java Quartz定時(shí)器任務(wù)與Spring task定時(shí)的幾種實(shí)現(xiàn)方法
- Java定時(shí)器Timer簡(jiǎn)述
- Java中Spring使用Quartz任務(wù)調(diào)度定時(shí)器
相關(guān)文章
詳解SpringBoot中添加@ResponseBody注解會(huì)發(fā)生什么
這篇文章主要介紹了詳解SpringBoot中添加@ResponseBody注解會(huì)發(fā)生什么,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11JavaCV與FFmpeg音視頻流處理技巧總結(jié)大全
JavaCV是一個(gè)開源的Java接口,它為幾個(gè)著名的計(jì)算機(jī)視覺庫(kù)(如OpenCV、FFmpeg)提供了Java封裝,這篇文章主要給大家介紹了關(guān)于JavaCV與FFmpeg音視頻流處理技巧總結(jié)的相關(guān)資料,需要的朋友可以參考下2024-05-05springboot1.X和2.X中如何解決Bean名字相同時(shí)覆蓋
這篇文章主要介紹了springboot1.X和2.X中如何解決Bean名字相同時(shí)覆蓋,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03Java并發(fā)線程之線程池的知識(shí)總結(jié)
這篇文章主要介紹了Java并發(fā)線程之線程池的知識(shí)總結(jié),幫助大家更好的理解和學(xué)習(xí)Java并發(fā)線程的相關(guān)內(nèi)容,感興趣的朋友可以了解下2021-01-01Java 流的高級(jí)使用之收集數(shù)據(jù)解析
這篇文章主要介紹了Java 流的高級(jí)使用之收集數(shù)據(jù)解析,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08Spring中的@CrossOrigin注冊(cè)處理方法源碼解析
這篇文章主要介紹了Spring中的@CrossOrigin注冊(cè)處理方法源碼解析,@CrossOrigin是基于@RequestMapping,@RequestMapping注釋方法掃描注冊(cè)的起點(diǎn)是equestMappingHandlerMapping.afterPropertiesSet(),需要的朋友可以參考下2023-12-12Springboot發(fā)送郵件功能的實(shí)現(xiàn)詳解
電子郵件是—種用電子手段提供信息交換的通信方式,是互聯(lián)網(wǎng)應(yīng)用最廣的服務(wù)。本文詳細(xì)為大家介紹了SpringBoot實(shí)現(xiàn)發(fā)送電子郵件功能的示例代碼,需要的可以參考一下2022-09-09SpringMVC基于注解方式實(shí)現(xiàn)上傳下載
本文主要介紹了SpringMVC基于注解方式實(shí)現(xiàn)上傳下載,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-04-04Java多線程下的其他組件之CyclicBarrier、Callable、Future和FutureTask詳解
這篇文章主要介紹了Java多線程下的其他組件之CyclicBarrier、Callable、Future和FutureTask詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07Springboot中用 Netty 開啟UDP服務(wù)方式
這篇文章主要介紹了Springboot中用 Netty 開啟UDP服務(wù)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11