欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

jdk自帶定時器使用方法詳解

 更新時間:2017年06月28日 10:54:56   作者:ToBeABetterPerson  
這篇文章主要為大家詳細介紹了jdk自帶定時器的使用方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下

首先看一下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

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • 詳解SpringBoot中添加@ResponseBody注解會發(fā)生什么

    詳解SpringBoot中添加@ResponseBody注解會發(fā)生什么

    這篇文章主要介紹了詳解SpringBoot中添加@ResponseBody注解會發(fā)生什么,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-11-11
  • JavaCV與FFmpeg音視頻流處理技巧總結大全

    JavaCV與FFmpeg音視頻流處理技巧總結大全

    JavaCV是一個開源的Java接口,它為幾個著名的計算機視覺庫(如OpenCV、FFmpeg)提供了Java封裝,這篇文章主要給大家介紹了關于JavaCV與FFmpeg音視頻流處理技巧總結的相關資料,需要的朋友可以參考下
    2024-05-05
  • springboot1.X和2.X中如何解決Bean名字相同時覆蓋

    springboot1.X和2.X中如何解決Bean名字相同時覆蓋

    這篇文章主要介紹了springboot1.X和2.X中如何解決Bean名字相同時覆蓋,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-03-03
  • Java并發(fā)線程之線程池的知識總結

    Java并發(fā)線程之線程池的知識總結

    這篇文章主要介紹了Java并發(fā)線程之線程池的知識總結,幫助大家更好的理解和學習Java并發(fā)線程的相關內(nèi)容,感興趣的朋友可以了解下
    2021-01-01
  • Java 流的高級使用之收集數(shù)據(jù)解析

    Java 流的高級使用之收集數(shù)據(jù)解析

    這篇文章主要介紹了Java 流的高級使用之收集數(shù)據(jù)解析,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-08-08
  • Spring中的@CrossOrigin注冊處理方法源碼解析

    Spring中的@CrossOrigin注冊處理方法源碼解析

    這篇文章主要介紹了Spring中的@CrossOrigin注冊處理方法源碼解析,@CrossOrigin是基于@RequestMapping,@RequestMapping注釋方法掃描注冊的起點是equestMappingHandlerMapping.afterPropertiesSet(),需要的朋友可以參考下
    2023-12-12
  • Springboot發(fā)送郵件功能的實現(xiàn)詳解

    Springboot發(fā)送郵件功能的實現(xiàn)詳解

    電子郵件是—種用電子手段提供信息交換的通信方式,是互聯(lián)網(wǎng)應用最廣的服務。本文詳細為大家介紹了SpringBoot實現(xiàn)發(fā)送電子郵件功能的示例代碼,需要的可以參考一下
    2022-09-09
  • SpringMVC基于注解方式實現(xiàn)上傳下載

    SpringMVC基于注解方式實現(xiàn)上傳下載

    本文主要介紹了SpringMVC基于注解方式實現(xiàn)上傳下載,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2022-04-04
  • Java多線程下的其他組件之CyclicBarrier、Callable、Future和FutureTask詳解

    Java多線程下的其他組件之CyclicBarrier、Callable、Future和FutureTask詳解

    這篇文章主要介紹了Java多線程下的其他組件之CyclicBarrier、Callable、Future和FutureTask詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-07-07
  • Springboot中用 Netty 開啟UDP服務方式

    Springboot中用 Netty 開啟UDP服務方式

    這篇文章主要介紹了Springboot中用 Netty 開啟UDP服務方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-11-11

最新評論