- 浏览: 86141 次
- 性别:
- 来自: 北京
-
文章分类
最新评论
-
ssy341:
说的简单了点,和官网的文档差不多,多文件上传也不是很好,不过可 ...
jQuery插件AjaxFileUpload文件上传实现Javascript多文件上传功能 -
蓝凝幽偌:
有没有jar包?
org.apache.commons.net.ftp.FTPClient 的简单用法 -
tinguo002:
在这里缓缓走过,很为楼主钻研精神感动,也感谢楼主分享。
关于Struts2资源文件的配置
用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));
}
如果要在程序中定时执行任务,可以使用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));
}
发表评论
-
java编程思想final的理解
2015-03-06 13:38 328根据上下文环境,java的关键字final也存在着细微的区别, ... -
PDF2SWF中文字符集解决方案
2014-02-26 15:12 801转自:http://www.cnblogs.com/liver ... -
Runtime.getRuntime().exec() 输出流阻塞的解决方法
2014-02-26 09:40 1824转自:http://blog.csdn.net/xiaoani ... -
笔记(不用老搜索了)
2013-07-17 16:16 0下面代码则执行了subgo()函数, <a href ... -
利用LinkedBlockingQueue实现生产者-消费者模式
2013-06-05 16:09 672由于LinkedBlockingQueue 实现是线程安全的, ... -
加密123
2013-05-08 22:35 795/** * 加密传输时的密码 * @author weirhp ... -
jsoup解析
2013-04-28 15:31 652http://www.ibm.com/developerwor ... -
java线程管理利器:java.util.current的用法举例
2012-12-05 15:42 980DK5中增加了Doug Lea的并发库,这一引进给java线程 ... -
Tomcat - 解决which must be escaped when used within the value错误
2012-11-19 15:17 632http://www.cnblogs.com/javadu/a ... -
Java POI Word 写文档
2012-11-09 09:19 8071 package apache.poi; 2 3 ... -
使用COS组件实现文件上传
2012-06-15 11:25 1463转自:http://blog.csdn.net/jadyer/ ... -
Java Annotation(1)
2011-06-19 09:48 638作者:曾巧(numenzq) 摘要 Annotation( ... -
关于request得到路径的几种方法
2011-06-08 15:43 922request.getRequestURI() /jque ... -
JAVA EXCEL API 简介
2011-04-18 16:32 701转自:http://www.ibm.com/dev ... -
Hibernate 多对多双向关联
2011-03-21 11:09 598Hibernate 多对多双向关联 一、模型介绍 多个 ... -
Hibernate 一对多连接表双向关联
2011-03-21 11:09 741Hibernate 一对多连接表双向关联 一、模型介绍 ... -
Hibernate 一对多外键双向关联
2011-03-21 11:08 753Hibernate 一对多外键双向关联 一、模型介绍 ... -
Hibernate 一对一主键双向关联
2011-03-21 11:07 685Hibernate 一对一主键双向关联 一对一主键映射在一 ... -
Hibernate 一对一外键双向关联
2011-03-21 11:07 619Hibernate 一对一外键双向关联 一对一外键关联是一 ... -
Hibernate 多对多单向关联
2011-03-21 11:06 614Hibernate 多对多单向关联 一、模型介绍 多个人( ...
相关推荐
boost steady_timer使用方法,
在本文中,我们将深入探讨如何使用C#编程语言创建Windows服务,并结合System.Timers.Timer类实现定时任务。这个实例不仅提供了源代码,还通过截图帮助理解每个步骤,这对于初学者和开发者来说是一份宝贵的资源。 ...
在本文中,我们将深入探讨`System.Timers.Timer`的使用以及如何解决可能出现的重入问题。 首先,`System.Timers.Timer`是.NET框架提供的一种计时器类型,适用于多线程环境。它通过.NET Thread Pool工作,能够在指定...
本教程将深入探讨如何使用`Timer`控件来显示实时的当前时间。 `Timer`控件是.NET框架中的一个控件,位于System.Windows.Forms命名空间下。在Windows Forms应用程序中,我们可以利用它来实现定时触发事件的功能,...
使用 `Timer` 的 `schedule()` 方法可以安排任务的执行。有两个主要的重载方法: 1. `schedule(TimerTask task, long delay)`:在指定的延迟(以毫秒为单位)后执行任务。 2. `schedule(TimerTask task, long delay,...
pb制作
应用程序通过操作系统提供的API来使用Timer。这可以是周期性动作,如心跳检测(Watchdog)、定期任务调度、计时器回调等。应用程序利用这些Timer实现各种功能,例如计时、延迟执行、超时检测等。 5. Timer in Qemu ...
NULL 博文链接:https://luan.iteye.com/blog/1838132
角度4-计时器我使用Angular4和Observable构建了Timer(开始/暂停,重置,等待)。 安装依赖 npm install启动开发服务器 npm start打开浏览器到运行单元测试 npm test运行端到端测试 npm run webdrivernpm run e2e
Java定时器Timer使用方法详解 Java中的定时器Timer是使用Timer类和TimerTask类来实现的,Timer类负责计划任务的功能,而TimerTask类则是封装要执行的任务的类。下面将详细介绍Java定时器Timer的使用方法。 一、...
下面将详细介绍`Timer`的使用及其相关知识点。 首先,`Timer`类并不是一个线程,它只是一个调度工具,用于安排在未来某个时间点执行的任务。`Timer`的工作是通过`TimerTask`类来完成的,`TimerTask`是实现了`...
1.一简单地过一下每个控件 1.最简单的示例 2.UpdatePanel 3.UpdateProcess 4.Timer 2.二客户端脚本编程 1.命名空间类成员接口继承枚举 3.三实践开发 1.用Ajax让GridView的行显示提示框
用vb6编写的一个用timer组件控制的满天星小程序.
这两种Timer虽然都与时间调度相关,但在使用上和特性上有所不同。在这个主题中,我们将深入探讨这两种Timer类如何在多线程环境下,特别是在Winform应用程序中,实现非阻塞的UI更新。 1. **System.Threading.Timer**...
采用实时调度,MatlabAS能用定时器功能完成时序调度.这个案例值得借鉴
【Timer定时器的使用方法】 在Java编程语言中,`java.util.Timer` 和 `java.util.TimerTask` 类是用于执行定期或延时任务的核心组件。它们允许开发者在指定的时间间隔内执行特定的操作,这对于实现定时任务,如定时...
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驱动的接口及使用方法,为Timer使用者提供参考。1.2 目标读者Timer 驱动层/应用层开发/使用/维护人员。1.3 适用范围表 1
本文将深入探讨如何使用C# `Timer`控件创建一个电子时钟,并将其应用于各种定时操作。 首先,`Timer`控件不是Windows Forms界面中的可见控件,但它可以在后台运行并周期性地触发事件。`System.Windows.Forms.Timer`...
最新单片机仿真 TIMER0与TIMER1控制条形LED最新单片机仿真 TIMER0与TIMER1控制条形LED最新单片机仿真 TIMER0与TIMER1控制条形LED最新单片机仿真 TIMER0与TIMER1控制条形LED最新单片机仿真 TIMER0与TIMER1控制条形LED...