【摘抄】关于Java schedule job Timer的schedule方法详解
2011-12-14 15:55
507 查看
Timer timer = new Timer(true); timer.schedule(TimerTask, delay, interval);
创建timer时使用new Timer(true)可以使timer线程成为一个daemon线程,这样当程序只有daemon线程的时候,它就会自动终止运行。
Timer的schedule函数有四种:
schedule(TimerTask task, long delay, long period) schedule(TimerTask task, Date time, long period) scheduleAtFixedRate(TimerTask task, long delay, long period) scheduleAtFixedRate(TimerTask task, Date firstTime, long period)
(1) 2个参数的schedule在制定任务计划时, 如果指定的计划执行时间scheduledExecutionTime<= systemCurrentTime,则task会被立即执行。scheduledExecutionTime不会因为某一个task的过度执行而改变。
(2) 3个参数的schedule在制定反复执行一个task的计划时,每一次执行这个task的计划执行时间随着前一次的实际执行时间而变,也就是 scheduledExecutionTime(第n+1次)=realExecutionTime(第n次)+periodTime。也就是说如果第n 次执行task时,由于某种原因这次执行时间过长,执行完后的systemCurrentTime>= scheduledExecutionTime(第n+1次),则此时不做时隔等待,立即执行第n+1次task,而接下来的第n+2次task的 scheduledExecutionTime(第n+2次)就随着变成了realExecutionTime(第n+1次)+periodTime。说 白了,这个方法更注重保持间隔时间的稳定。
(3)3个参数的scheduleAtFixedRate在制定反复执行一个task的计划时,每一次 执行这个task的计划执行时间在最初就被定下来了,也就是scheduledExecutionTime(第n次)=firstExecuteTime +n*periodTime;如果第n次执行task时,由于某种原因这次执行时间过长,执行完后的systemCurrentTime>= scheduledExecutionTime(第n+1次),则此时不做period间隔等待,立即执行第n+1次task,而接下来的第n+2次的 task的scheduledExecutionTime(第n+2次)依然还是firstExecuteTime+(n+2)*periodTime这 在第一次执行task就定下来了。说白了,这个方法更注重保持执行频率的稳定。
相关文章推荐
- java中timer的schedule和scheduleAtFixedRate方法区别详解
- 关于java中可变长参数的定义及使用方法详解
- 简单理解java中timer的schedule和scheduleAtFixedRate方法的区别
- timer.schedule方法中参数详解
- java.util.timer的 schedule方法
- java关于Timer schedule执行定时任务
- 关于Java代码中一个方法代码超出65535字节详解
- java关于Timer schedule执行定时任务
- java Timer 的 schedule 和 scheduleAtFixedRate 方法的区别
- java中关于Map的三种遍历方法详解
- java关于Timer schedule执行定时任务
- [java]Timer的schedule和scheduleAtFixedRate方法的区别解析
- java关于Timer schedule执行定时任务
- java中关于Map的三种遍历方法机putAll的用法详解
- 定时器之timer.schedule()方法详解
- timer.schedule方法中参数详解
- java关于Timer schedule执行定时任务 1、在应用开发中,经常需要一些周期性的操作,比如每5分钟执行某一操作等
- java关于Timer schedule执行定时任务
- java关于Timer schedule执行定时任务
- java关于Timer schedule执行定时任务