`
tianyuzhu
  • 浏览: 24748 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

timer

阅读更多

      如果要在程序中定时执行任务,可以使用java.util.Timer这个类实现。使用Timer类需要一个继承了java.util.TimerTask的类。TimerTask是一个虚类,需要实现它的run方法,实际上是他implements了Runnable接口,而把run方法留给子类实现。
      下面是我的一个例子:

class Worker extends TimerTask {
  
public void run() {
    System.
out.println("我在工作啦!");
  }

}


      Timer类用schedule方法或者scheduleAtFixedRate方法启动定时执行,schedule重载了四个版本,scheduleAtFixedRate重载了两个。每个方法的实现都不同,下面是每个方法的说明:

schedule

public void schedule(TimerTask task,
                     long delay)
Schedules the specified task for execution after the specified delay.

 

Parameters:
task - task to be scheduled.
delay - delay in milliseconds before task is to be executed.
Throws:
IllegalArgumentException - if delay is negative, or delay + System.currentTimeMillis() is negative.
IllegalStateException - if task was already scheduled or cancelled, or timer was cancelled.

说明:该方法会在设定的延时后执行一次任务。


<!-- -->

schedule

public void schedule(TimerTask task,
                     Date time)
Schedules the specified task for execution at the specified time. If the time is in the past, the task is scheduled for immediate execution.

 

Parameters:
task - task to be scheduled.
time - time at which task is to be executed.
Throws:
IllegalArgumentException - if time.getTime() is negative.
IllegalStateException - if task was already scheduled or cancelled, timer was cancelled, or timer thread terminated.

说明:该方法会在指定的时间点执行一次任务。


<!-- -->

schedule

public void schedule(TimerTask task,
                     long delay,
                     long period)
Schedules the specified task for repeated fixed-delay execution, beginning after the specified delay. Subsequent executions take place at approximately regular intervals separated by the specified period.

In fixed-delay execution, each execution is scheduled relative to the actual execution time of the previous execution. If an execution is delayed for any reason (such as garbage collection or other background activity), subsequent executions will be delayed as well. In the long run, the frequency of execution will generally be slightly lower than the reciprocal of the specified period (assuming the system clock underlying Object.wait(long) is accurate).

Fixed-delay execution is appropriate for recurring activities that require "smoothness." In other words, it is appropriate for activities where it is more important to keep the frequency accurate in the short run than in the long run. This includes most animation tasks, such as blinking a cursor at regular intervals. It also includes tasks wherein regular activity is performed in response to human input, such as automatically repeating a character as long as a key is held down.

 

Parameters:
task - task to be scheduled.
delay - delay in milliseconds before task is to be executed.
period - time in milliseconds between successive task executions.
Throws:
IllegalArgumentException - if delay is negative, or delay + System.currentTimeMillis() is negative.
IllegalStateException - if task was already scheduled or cancelled, timer was cancelled, or timer thread terminated.

说明:该方法会在指定的延时后执行任务,并且在设定的周期定时执行任务。


<!-- -->

schedule

public void schedule(TimerTask task,
                     Date firstTime,
                     long period)
Schedules the specified task for repeated fixed-delay execution, beginning at the specified time. Subsequent executions take place at approximately regular intervals, separated by the specified period.

In fixed-delay execution, each execution is scheduled relative to the actual execution time of the previous execution. If an execution is delayed for any reason (such as garbage collection or other background activity), subsequent executions will be delayed as well. In the long run, the frequency of execution will generally be slightly lower than the reciprocal of the specified period (assuming the system clock underlying Object.wait(long) is accurate).

Fixed-delay execution is appropriate for recurring activities that require "smoothness." In other words, it is appropriate for activities where it is more important to keep the frequency accurate in the short run than in the long run. This includes most animation tasks, such as blinking a cursor at regular intervals. It also includes tasks wherein regular activity is performed in response to human input, such as automatically repeating a character as long as a key is held down.

 

Parameters:
task - task to be scheduled.
firstTime - First time at which task is to be executed.
period - time in milliseconds between successive task executions.
Throws:
IllegalArgumentException - if time.getTime() is negative.
IllegalStateException - if task was already scheduled or cancelled, timer was cancelled, or timer thread terminated.

说明:该方法会在指定的时间点执行任务,然后从该时间点开始,在设定的周期定时执行任务。特别的,如果设定的时间点在当前时间之前,任务会被马上执行,然后开始按照设定的周期定时执行任务。


<!-- -->

scheduleAtFixedRate

public void scheduleAtFixedRate(TimerTask task,
                                long delay,
                                long period)
Schedules the specified task for repeated fixed-rate execution, beginning after the specified delay. Subsequent executions take place at approximately regular intervals, separated by the specified period.

In fixed-rate execution, each execution is scheduled relative to the scheduled execution time of the initial execution. If an execution is delayed for any reason (such as garbage collection or other background activity), two or more executions will occur in rapid succession to "catch up." In the long run, the frequency of execution will be exactly the reciprocal of the specified period (assuming the system clock underlying Object.wait(long) is accurate).

Fixed-rate execution is appropriate for recurring activities that are sensitive to absolute time, such as ringing a chime every hour on the hour, or running scheduled maintenance every day at a particular time. It is also appropriate for recurring activities where the total time to perform a fixed number of executions is important, such as a countdown timer that ticks once every second for ten seconds. Finally, fixed-rate execution is appropriate for scheduling multiple repeating timer tasks that must remain synchronized with respect to one another.

 

Parameters:
task - task to be scheduled.
delay - delay in milliseconds before task is to be executed.
period - time in milliseconds between successive task executions.
Throws:
IllegalArgumentException - if delay is negative, or delay + System.currentTimeMillis() is negative.
IllegalStateException - if task was already scheduled or cancelled, timer was cancelled, or timer thread terminated.

说明:该方法和schedule的相同参数的版本类似,不同的是,如果该任务因为某些原因(例如垃圾收集)而延迟执行,那么接下来的任务会尽可能的快速执行,以赶上特定的时间点。


<!-- -->

scheduleAtFixedRate

public void scheduleAtFixedRate(TimerTask task,
                                Date firstTime,
                                long period)
Schedules the specified task for repeated fixed-rate execution, beginning at the specified time. Subsequent executions take place at approximately regular intervals, separated by the specified period.

In fixed-rate execution, each execution is scheduled relative to the scheduled execution time of the initial execution. If an execution is delayed for any reason (such as garbage collection or other background activity), two or more executions will occur in rapid succession to "catch up." In the long run, the frequency of execution will be exactly the reciprocal of the specified period (assuming the system clock underlying Object.wait(long) is accurate).

Fixed-rate execution is appropriate for recurring activities that are sensitive to absolute time, such as ringing a chime every hour on the hour, or running scheduled maintenance every day at a particular time. It is also appropriate for recurring activities where the total time to perform a fixed number of executions is important, such as a countdown timer that ticks once every second for ten seconds. Finally, fixed-rate execution is appropriate for scheduling multiple repeating timer tasks that must remain synchronized with respect to one another.

 

Parameters:
task - task to be scheduled.
firstTime - First time at which task is to be executed.
period - time in milliseconds between successive task executions.
Throws:
IllegalArgumentException - if time.getTime() is negative.
IllegalStateException - if task was already scheduled or cancelled, timer was cancelled, or timer thread terminated.

说明:和上一个方法类似。

      下面是我的一个测试片断:

  public static void main(String[] args) throws Exception {
    Timer timer 
= new Timer(false);
    timer.schedule(
new Worker(), new Date(System.currentTimeMillis() + 1000));
  }
分享到:
评论

相关推荐

    C#中Forms.Timer、Timers.Timer、Threading.Timer的用法分析

    本文实例讲述了C#中Forms.Timer、Timers.Timer、Threading.Timer的用法分析,分享给大家供大家参考。具体分析如下: 在.NET Framework里面提供了三种Timer ① System.Windows.Forms.Timer ② System.Timers.Timer ③...

    Timer_基于Verilog的timer计时器_

    **基于Verilog的Timer计时器详解** 在数字系统设计中,Timer是一个至关重要的模块,它用于实现定时或延时功能。在这个项目中,我们关注的是一个基于Verilog硬件描述语言实现的Timer计时器。Verilog是一种广泛使用的...

    02-Timer0-Timer1-Timer2-Timer3-Timer4测试程序.rar

    这个"02-Timer0-Timer1-Timer2-Timer3-Timer4测试程序.rar"压缩包包含了一系列针对STC8A8K芯片上所有定时器的测试程序,这些程序对于理解和应用这些定时器功能至关重要。 STC8A8K系列单片机提供了多个定时器,包括...

    加Timer控件 timer1

    ### Timer 控件在C#中的应用 #### 一、Timer 控件简介 在C#的Windows Forms应用程序开发中,`System.Windows.Forms.Timer`(通常简称为Timer)是一种非常实用的控件,它能够周期性地执行某个方法或事件处理程序。...

    timer_hw.rar_hw_timer

    在IT行业中,硬件定时器(HW Timer)是嵌入式系统和计算机硬件中不可或缺的组件。硬件定时器通常由微控制器或系统级芯片(SoC)中的专用硬件电路实现,用于执行时间相关的任务,如中断服务、周期性操作或者精确计时...

    4 Timer0定时器中断_TIMER0_c8051f340_

    本文将深入探讨C8051F340中的TIMER0定时器中断,以及如何利用它实现所需的系统功能。 TIMER0是C8051F340内部的一种硬件定时器资源,常用于时间间隔的测量、脉冲宽度调制(PWM)生成、波特率发生器等多种应用。它...

    C语言02-Timer0-Timer1-Timer2-Timer3-Timer4测试程序(STC32G-DEMO-CODE-22

    C语言02-Timer0-Timer1-Timer2-Timer3-Timer4测试程序(STC32G-DEMO-CODE-220311kw)C语言02-Timer0-Timer1-Timer2-Timer3-Timer4测试程序(STC32G-DEMO-CODE-220311kw)C语言02-Timer0-Timer1-Timer2-Timer3-Timer4...

    单片机C语言程序设计 TIMER0与TIMER1控制条形LED(有源码)

    单片机C语言程序设计 TIMER0与TIMER1控制条形LED(有源码)单片机C语言程序设计 TIMER0与TIMER1控制条形LED(有源码)单片机C语言程序设计 TIMER0与TIMER1控制条形LED(有源码)单片机C语言程序设计 TIMER0与TIMER1控制...

    MsTimer2.rar

    MsTimer2是一个专门为Arduino平台设计的高级定时器库,它扩展了Arduino的定时功能,提供了比内置`millis()`和`delay()`函数更精确、更灵活的定时解决方案。这个库特别适合那些需要高精度定时任务或者同时管理多个...

    C#定时器(Timer)

    C#定时器(Timer)是.NET框架中一个非常重要的组件,它允许开发者在特定的时间间隔内执行特定的代码块,从而实现周期性的任务。在Windows应用程序、服务或控制台程序中,C#定时器常常被用来创建后台任务、监控、更新...

    最新单片机仿真 TIMER0与TIMER1控制条形LED

    最新单片机仿真 TIMER0与TIMER1控制条形LED最新单片机仿真 TIMER0与TIMER1控制条形LED最新单片机仿真 TIMER0与TIMER1控制条形LED最新单片机仿真 TIMER0与TIMER1控制条形LED最新单片机仿真 TIMER0与TIMER1控制条形LED...

    系统Timer机制,从硬件到操作系统,还有Qemu对timer的模拟

    本文将深入探讨Timer的各个层面,包括Timer机制、硬件Timer、操作系统中的Timer、应用程序中的Timer以及在QEMU中的Timer模拟。 1. Timer Mechanism Timer机制是计算机系统中用于定时和计时的基础结构。它通常由多层...

    5 Timer2定时器中断_c8051f340_TIMER2定时器中断_

    在这款芯片中,TIMER2是一个重要的定时/计数器资源,它提供了中断功能,使得开发者能够实现基于时间的事件处理和精确的时序控制。下面我们将详细探讨TIMER2定时器中断的相关知识点。 1. TIMER2结构与功能: TIMER2...

    多个 timer 使用示例

    标题 "多个 timer 使用示例" 暗示我们即将探讨的是如何在编程环境中,特别是使用 Progress 4GL(通常简称为 PB,即 PowerBuilder)语言,有效地管理并使用多个计时器(timer)对象。Progress 4GL 是一种面向对象的...

    arm generic timer prelim data

    根据提供的文档信息,我们可以深入探讨ARM Generic Timer的相关知识点。标题和描述均提到“ARM Generic Timer Prelim Data”,这意味着文档提供的是关于ARM通用定时器的一些初步数据和技术规范。下面将详细介绍ARM ...

    異步Timer Timer

    在这个场景中,"異步Timer Timer"的标题和描述提到了时间管理和异步处理事件的概念,这通常与编程语言中的定时器控件有关。我们将深入探讨异步编程、VB(Visual Basic)中的Timer控件以及它们如何协同工作。 首先,...

    cmsdk_apb_timer_M3verilog_SOC_apb_timer_apbverilog_CMSDK_

    标题 "cmsdk_apb_timer_M3verilog_SOC_apb_timer_apbverilog_CMSDK_" 暗示了这是一个基于Verilog语言设计的定时器模块,适用于ARM Cortex-M3架构的系统级芯片(SoC)设计。CMSDK可能指的是Core Micro-System Design ...

    C#中Timer使用及解决重入问题

    在C#编程中,`Timer`是一个非常常用的组件,它允许开发者在指定的时间间隔内触发一个事件。在本文中,我们将深入探讨`System.Timers.Timer`的使用以及如何解决可能出现的重入问题。 首先,`System.Timers.Timer`是...

    dw_apb_timer_of.rar_V2 _apb timer_dw_apb_dw_timer_pc3x2

    【标题】"dw_apb_timer_of.rar_V2_apb timer_dw_apb_dw_timer_pc3x2" 提供的是一款基于Linux内核v2.13.6的APB定时器驱动程序,它主要关注DW_APB(DesignWare APB)定时器的实现。DW_APB定时器是一种常见的数字信号...

Global site tag (gtag.js) - Google Analytics