最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

首先看一下jdk自帶定時器:

一種工具,線程用其安排以后在后臺線程中執(zhí)行的任務(wù)。可安排任務(wù)執(zhí)行一次,或者定期重復(fù)執(zhí)行。與每個 Timer 對象相對應(yīng)的是單個后臺線程,用于順序地執(zhí)行所有計時器任務(wù)。計時器任務(wù)應(yīng)該迅速完成。如果完成某個計時器任務(wù)的時間太長,那么它會“獨占”計時器的任務(wù)執(zhí)行線程。因此,這就可能延遲后續(xù)任務(wù)的執(zhí)行,而這些任務(wù)就可能“堆在一起”,并且在上述不友好的任務(wù)最終完成時才能夠被快速連續(xù)地執(zhí)行。

schedule(TimerTask task,long delay) 安排在指定延遲后執(zhí)行指定的任務(wù)。
schedule(TimerTask task,Date time) 安排在指定的時間執(zhí)行指定的任務(wù)。如果此時間已過去,則安排立即執(zhí)行該任務(wù)。
schedule(TimerTask task, long delay, long period) 安排指定的任務(wù)從指定的延遲后開始進行重復(fù)的固定延遲執(zhí)行。如果由于任何原因(如垃圾回收或其他后臺活動)而延遲了某次執(zhí)行,則后續(xù)執(zhí)行也將被延遲
schedule(TimerTask task,Date firstTime,long period) 安排指定的任務(wù)在指定的時間開始進行重復(fù)的固定延遲執(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次后關(guān)閉定時器
        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次后關(guān)閉定時器
        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

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

相關(guān)文章

  • mybatis resultmap 如何為對象賦值的調(diào)用順序

    mybatis resultmap 如何為對象賦值的調(diào)用順序

    這篇文章主要介紹了mybatis resultmap 如何為對象賦值的調(diào)用順序,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-01-01
  • Spring實現(xiàn)郵件發(fā)送功能

    Spring實現(xiàn)郵件發(fā)送功能

    這篇文章主要為大家詳細介紹了Spring實現(xiàn)郵件發(fā)送功能,簡單的發(fā)送郵件工具JavaMailSender使用,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-11-11
  • Spring與Mybatis的整合方法有哪些

    Spring與Mybatis的整合方法有哪些

    本文主要給大家介紹Spring與Mybatis的三種常用整合方法,需要用到的整合框架包mybatis-spring.jar,對spring mybatis整合感興趣的朋友可以參考下本文
    2015-10-10
  • MyBatis-Plus Sequence主鍵的實現(xiàn)

    MyBatis-Plus Sequence主鍵的實現(xiàn)

    這篇文章主要介紹了MyBatis-Plus Sequence主鍵的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-12-12
  • java更改圖片大小示例分享

    java更改圖片大小示例分享

    這篇文章主要介紹了java更改圖片大小示例,方法中指定路徑 ,舊文件名稱 ,新文件名稱,n 改變倍數(shù)就可以完成更改圖片大小,需要的朋友可以參考下
    2014-03-03
  • 一篇文章帶你入門java注解

    一篇文章帶你入門java注解

    這篇文章主要介紹了Java注解詳細介紹,本文講解了Java注解是什么、Java注解基礎(chǔ)知識、Java注解類型、定義Java注解類型的注意事項等內(nèi)容,需要的朋友可以參考下
    2021-08-08
  • mybatis-plus id主鍵生成的坑

    mybatis-plus id主鍵生成的坑

    這篇文章主要介紹了mybatis-plus id主鍵生成的坑,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-08-08
  • Spring?Boot?Reactor?整合?Resilience4j詳析

    Spring?Boot?Reactor?整合?Resilience4j詳析

    這篇文章主要介紹了Spring?Boot?Reactor整合Resilience4j詳析,文章通過引入pom包展開詳細介紹,具有一定的參考價值,感興趣的小伙伴可以參考一下
    2022-09-09
  • Java中static作用詳解

    Java中static作用詳解

    這篇文章主要介紹了Java中static作用,static表示“全局”或者“靜態(tài)”的意思,用來修飾成員變量和成員方法,也可以形成靜態(tài)static代碼塊,需要的朋友可以參考下
    2015-09-09
  • Java中文件寫入內(nèi)容的幾種常見方法

    Java中文件寫入內(nèi)容的幾種常見方法

    本文主要介紹了Java中文件寫入內(nèi)容的幾種常見方法,主要包括使用NIO的Files工具類、通過commons-io的FileUtils工具類、RandomAccessFile、PrintWriter和BufferedWriter這幾種,具有一定的參考價值,感興趣的可以了解一下
    2023-10-10

最新評論

贵定县| 同心县| 北海市| 沐川县| 尼玛县| 云林县| 沁源县| 淅川县| 新邵县| 明溪县| 永新县| 江源县| 博兴县| 余姚市| 哈密市| 吉木乃县| 宜昌市| 邵武市| 平顺县| 白朗县| 江北区| 顺昌县| 托里县| 崇文区| 仁怀市| 平阳县| 阳朔县| 武乡县| 汉寿县| 巍山| 阳东县| 寿宁县| 万盛区| 萨迦县| 怀化市| 宁德市| 奉节县| 翼城县| 泗洪县| 宁国市| 于都县|