jdk自带定时器使用方法详解

 更新时间:2017年06月28日 10:54:56   作者:ToBeABetterPerson  
这篇文章主要为大家详细介绍了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) 安排指定的任务在指定的时间开始进行重复的固定延迟执行。如果由于任何原因(如垃圾回收或其他后台活动)而延迟了某次执行,则后续执行也将被延迟。

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后执行定时器,仅执行一次
    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秒后执行定时器,每1s执行一次
    System.out.print(sdf.format(new Date()));
    System.out.println("the timer two will be executed after 12 seconds...");
    //启动后延迟时间
    long afterSs = 12 * 1000;
    //执行周期
    long intervalSs1 = 1 * 1000;
    timer.schedule(new TimerTask() {
      // 执行计数器
      int i = 0;

      @Override
      public void run() {
        System.out.print(sdf.format(new Date()));
        System.out.println("the timer two has execution " + (++i) + " timers");
        // 执行10次后关闭定时器
        if (i == 10) {
          this.cancel();
        }
      }
    }, afterSs, intervalSs1);
    
    
    // 指定时间执行定时器,仅执行一次
    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);
    
    // 从指定时间开始周期性执行
    System.out.print(sdf.format(new Date()));
    System.out.println("the timer four will be executed at 2017-06-27 21:48:00...");
    // 执行间隔周期
    long intervalSs = 1 * 1000;
    // 开始执行时间
    Date beginTime = sdf.parse("2017-06-27 21:48:00");
    timer.schedule(new TimerTask() {
      // 执行计数器
      int i = 0;

      @Override
      public void run() {
        System.out.print(sdf.format(new Date()));
        System.out.println("the timer four has execution " + (++i) + " timers");
        // 执行10次后关闭定时器
        if (i == 10) {
          this.cancel();
        }
      }
    }, beginTime, intervalSs);
  }

}

执行结果

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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • mybatis resultmap 如何为对象赋值的调用顺序

    mybatis resultmap 如何为对象赋值的调用顺序

    这篇文章主要介绍了mybatis resultmap 如何为对象赋值的调用顺序,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-01-01
  • Spring实现邮件发送功能

    Spring实现邮件发送功能

    这篇文章主要为大家详细介绍了Spring实现邮件发送功能,简单的发送邮件工具JavaMailSender使用,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-11-11
  • Spring与Mybatis的整合方法有哪些

    Spring与Mybatis的整合方法有哪些

    本文主要给大家介绍Spring与Mybatis的三种常用整合方法,需要用到的整合框架包mybatis-spring.jar,对spring mybatis整合感兴趣的朋友可以参考下本文
    2015-10-10
  • MyBatis-Plus Sequence主键的实现

    MyBatis-Plus Sequence主键的实现

    这篇文章主要介绍了MyBatis-Plus Sequence主键的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-12-12
  • java更改图片大小示例分享

    java更改图片大小示例分享

    这篇文章主要介绍了java更改图片大小示例,方法中指定路径 ,旧文件名称 ,新文件名称,n 改变倍数就可以完成更改图片大小,需要的朋友可以参考下
    2014-03-03
  • 一篇文章带你入门java注解

    一篇文章带你入门java注解

    这篇文章主要介绍了Java注解详细介绍,本文讲解了Java注解是什么、Java注解基础知识、Java注解类型、定义Java注解类型的注意事项等内容,需要的朋友可以参考下
    2021-08-08
  • mybatis-plus id主键生成的坑

    mybatis-plus id主键生成的坑

    这篇文章主要介绍了mybatis-plus id主键生成的坑,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    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表示“全局”或者“静态”的意思,用来修饰成员变量和成员方法,也可以形成静态static代码块,需要的朋友可以参考下
    2015-09-09
  • Java中文件写入内容的几种常见方法

    Java中文件写入内容的几种常见方法

    本文主要介绍了Java中文件写入内容的几种常见方法,主要包括使用NIO的Files工具类、通过commons-io的FileUtils工具类、RandomAccessFile、PrintWriter和BufferedWriter这几种,具有一定的参考价值,感兴趣的可以了解一下
    2023-10-10

最新评论