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

发布时间 - 2026-01-11 02:05:50    点击率:

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

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


# jdk  # 定时器  # java当中的定时器的4种使用方式  # JAVA中 Spring定时器的两种实现方式  # java使用TimerTask定时器获取指定网络数据  # Java 定时器(Timer  # TimerTask)详解及实例代码  # 解析Java中的定时器及使用定时器制作弹弹球游戏的示例  # Java 定时器(Timer)及线程池里使用定时器实例代码  # java实现多线程之定时器任务  # java Quartz定时器任务与Spring task定时的几种实现方法  # Java定时器Timer简述  # Java中Spring使用Quartz任务调度定时器  # 计时器  # 将被  # 或其他  # 自带  # 排在  # 的是  # 执行时间  # 这就  # 看一下  # 它会  # 时才  # 太长  # 相对应  # 大家多多  # 不友好  # 延迟时间  # 在上述  # import  # test  # ParseException 


相关栏目: 【 网站优化151355 】 【 网络推广146373 】 【 网络技术251813 】 【 AI营销90571


相关推荐: 如何在万网开始建站?分步指南解析  高配服务器限时抢购:企业级配置与回收服务一站式优惠方案  Swift中swift中的switch 语句  Laravel如何实现图片防盗链功能_Laravel中间件验证Referer来源请求【方案】  Laravel辅助函数有哪些_Laravel Helpers常用助手函数大全  香港服务器WordPress建站指南:SEO优化与高效部署策略  JavaScript如何实现路由_前端路由原理是什么  JS中对数组元素进行增删改移的方法总结  如何在建站宝盒中设置产品搜索功能?  C#如何调用原生C++ COM对象详解  HTML透明颜色代码怎么让图片透明_给img元素加透明色的技巧【方法】  网站建设整体流程解析,建站其实很容易!  Bootstrap CSS布局之列表  php做exe能调用系统命令吗_执行cmd指令实现方式【详解】  如何用美橙互联一键搭建多站合一网站?  如何在Windows环境下新建FTP站点并设置权限?  如何在阿里云ECS服务器部署织梦CMS网站?  大连 网站制作,大连天途有线官网?  Laravel如何创建自定义中间件?(Middleware代码示例)  香港代理服务器配置指南:高匿IP选择、跨境加速与SEO优化技巧  邀请函制作网站有哪些,有没有做年会邀请函的网站啊?在线制作,模板很多的那种?  在centOS 7安装mysql 5.7的详细教程  电商网站制作多少钱一个,电子商务公司的网站制作费用计入什么科目?  Gemini手机端怎么发图片_Gemini手机端发图方法【步骤】  Laravel怎么实现微信登录_Laravel Socialite第三方登录集成  如何在 Python 中将列表项按字母顺序编号(a.、b.、c. …)  矢量图网站制作软件,用千图网的一张矢量图做公司app首页,该网站并未说明版权等问题,这样做算不算侵权?应该如何解决?  香港服务器租用费用高吗?如何避免常见误区?  Android使用GridView实现日历的简单功能  Laravel如何实现API版本控制_Laravel API版本化路由设计策略  Laravel集合Collection怎么用_Laravel集合常用函数详解  广州网站制作公司哪家好一点,广州欧莱雅百库网络科技有限公司官网?  详解CentOS6.5 安装 MySQL5.1.71的方法  php嵌入式断网后怎么恢复_php检测网络重连并恢复硬件控制【操作】  Win11怎么更改系统语言为中文_Windows11安装语言包并设为显示语言  PythonWeb开发入门教程_Flask快速构建Web应用  js实现点击每个li节点,都弹出其文本值及修改  Laravel怎么设置路由分组Prefix_Laravel多级路由嵌套与命名空间隔离【步骤】  Laravel如何使用withoutEvents方法临时禁用模型事件  如何挑选优质建站一级代理提升网站排名?  如何在阿里云虚拟服务器快速搭建网站?  EditPlus中的正则表达式实战(6)  Win11摄像头无法使用怎么办_Win11相机隐私权限开启教程【详解】  如何在景安云服务器上绑定域名并配置虚拟主机?  微信小程序 input输入框控件详解及实例(多种示例)  西安市网站制作公司,哪个相亲网站比较好?西安比较好的相亲网站?  Laravel中间件起什么作用_Laravel Middleware请求生命周期与自定义详解  Laravel如何实现登录错误次数限制_Laravel自带LoginThrottles限流配置【方法】  Laravel怎么在Controller之外的地方验证数据  Laravel如何实现URL美化Slug功能_Laravel使用eloquent-sluggable生成别名【方法】