`
sdustyongz
  • 浏览: 86141 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Timer的使用

阅读更多
用java.util.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));
  }
分享到:
评论

相关推荐

    steady_timer使用

    boost steady_timer使用方法,

    C#创建windows服务搭配定时器Timer使用实例(用代码做,截图版)

    在本文中,我们将深入探讨如何使用C#编程语言创建Windows服务,并结合System.Timers.Timer类实现定时任务。这个实例不仅提供了源代码,还通过截图帮助理解每个步骤,这对于初学者和开发者来说是一份宝贵的资源。 ...

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

    在本文中,我们将深入探讨`System.Timers.Timer`的使用以及如何解决可能出现的重入问题。 首先,`System.Timers.Timer`是.NET框架提供的一种计时器类型,适用于多线程环境。它通过.NET Thread Pool工作,能够在指定...

    C# 使用Timer控件显示当前时间

    本教程将深入探讨如何使用`Timer`控件来显示实时的当前时间。 `Timer`控件是.NET框架中的一个控件,位于System.Windows.Forms命名空间下。在Windows Forms应用程序中,我们可以利用它来实现定时触发事件的功能,...

    Java计时器Timer 使用.doc

    使用 `Timer` 的 `schedule()` 方法可以安排任务的执行。有两个主要的重载方法: 1. `schedule(TimerTask task, long delay)`:在指定的延迟(以毫秒为单位)后执行任务。 2. `schedule(TimerTask task, long delay,...

    多个 timer 使用示例

    pb制作

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

    应用程序通过操作系统提供的API来使用Timer。这可以是周期性动作,如心跳检测(Watchdog)、定期任务调度、计时器回调等。应用程序利用这些Timer实现各种功能,例如计时、延迟执行、超时检测等。 5. Timer in Qemu ...

    定时任务Timer使用

    NULL 博文链接:https://luan.iteye.com/blog/1838132

    Angular4-Timer:Angular4 Timer使用可观察的

    角度4-计时器我使用Angular4和Observable构建了Timer(开始/暂停,重置,等待)。 安装依赖 npm install启动开发服务器 npm start打开浏览器到运行单元测试 npm test运行端到端测试 npm run webdrivernpm run e2e

    Java定时器Timer使用方法详解

    Java定时器Timer使用方法详解 Java中的定时器Timer是使用Timer类和TimerTask类来实现的,Timer类负责计划任务的功能,而TimerTask类则是封装要执行的任务的类。下面将详细介绍Java定时器Timer的使用方法。 一、...

    android中timer的使用

    下面将详细介绍`Timer`的使用及其相关知识点。 首先,`Timer`类并不是一个线程,它只是一个调度工具,用于安排在未来某个时间点执行的任务。`Timer`的工作是通过`TimerTask`类来完成的,`TimerTask`是实现了`...

    系统学习asp.net Ajax中GridView、UpdatePanel、UpdateProgress、Timer使用 .

    1.一简单地过一下每个控件 1.最简单的示例 2.UpdatePanel 3.UpdateProcess 4.Timer 2.二客户端脚本编程 1.命名空间类成员接口继承枚举 3.三实践开发 1.用Ajax让GridView的行显示提示框

    vb6 timer组件应用

    用vb6编写的一个用timer组件控制的满天星小程序.

    C# Timer的多线程使用方法

    这两种Timer虽然都与时间调度相关,但在使用上和特性上有所不同。在这个主题中,我们将深入探讨这两种Timer类如何在多线程环境下,特别是在Winform应用程序中,实现非阻塞的UI更新。 1. **System.Threading.Timer**...

    timer使用案例

    采用实时调度,MatlabAS能用定时器功能完成时序调度.这个案例值得借鉴

    Timer定时器的使用方法

    【Timer定时器的使用方法】 在Java编程语言中,`java.util.Timer` 和 `java.util.TimerTask` 类是用于执行定期或延时任务的核心组件。它们允许开发者在指定的时间间隔内执行特定的操作,这对于实现定时任务,如定时...

    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...

    RTOS_Timer_开发指南1

    介绍RTOS中Timer驱动的接口及使用方法,为Timer使用者提供参考。1.2 目标读者Timer 驱动层/应用层开发/使用/维护人员。1.3 适用范围表 1

    C# timer控件使用

    本文将深入探讨如何使用C# `Timer`控件创建一个电子时钟,并将其应用于各种定时操作。 首先,`Timer`控件不是Windows Forms界面中的可见控件,但它可以在后台运行并周期性地触发事件。`System.Windows.Forms.Timer`...

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

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

Global site tag (gtag.js) - Google Analytics