schedule和scheduleAtFixedRate区别
来源:互联网 发布:属下知错 请主人加罚 编辑:程序博客网 时间:2024/05/26 16:01
来自:
http://hi.baidu.com/hemes1314/blog/item/359c089466c4d413d21b70dc.html
schedule和scheduleAtFixedRate 区别:
(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就定下来了。说白了,这个方法更注重保持执行频率的稳定。
@—@—@————@@@
Timer的实例:
package com.hemes.timer;
import java.util.*;
public class doTask extends TimerTask {
// true时使用后台进程线程。只要剩下的程序记叙运行,后台进程线程就会执行。
Timer myTimer;
public void start(int delay, int hour) {
myTimer = new Timer();
myTimer.schedule(this, delay * 1000, hour*1000*60*60); //利用timer.schedule方法
//public void schedule(TimerTask task,long time,long period)
//task被安排在延迟time后执行,执行后将每隔period(毫秒)反复执行。由于规定的时间间隔并不能保证与时钟精准的同不步,所以该方
}
public void start(Date time, int hour) {
myTimer = new Timer();
myTimer.schedule(this, time, hour*1000*60*60); //利用timer.schedule方法
//public void schedule(TimerTask task,Date time,long period)
//task被安排在time指定的时间执行,执行后将每隔period(毫秒)反复执行。由于规定的时间间隔并不能保证与时钟精准的同不步,所以该方
}
public void run() {
//执行任务(sql)
System.out.println("do Task...");
}
public void end(){
myTimer.cancel();
//终止Timer的功能执行,但不会对正在执行的任务有影响。当执行cancel方法后将不能再用其分配任务。
}
//测试
public static void main(String args[]) {
doTask myTask1 = new doTask();
//Get the Date corresponding to 11:30:00 pm today.
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 23);
calendar.set(Calendar.MINUTE, 30);
calendar.set(Calendar.SECOND, 0);
Date time = calendar.getTime();
myTask1.start(time,24);
/*
try{
Thread.sleep(6000);//将当前线程挂起指定的时间(线程运行时间)
}
catch(InterruptedException e){
}*/
//myTask1.end();//线程结束
}
}
- schedule和scheduleAtFixedRate区别
- schedule和scheduleAtFixedRate区别
- schedule和scheduleAtFixedRate区别
- schedule和scheduleAtFixedRate区别
- schedule和scheduleAtFixedRate区别
- schedule() 和 scheduleAtFixedRate() 区别
- schedule和scheduleAtFixedRate的区别
- Time schedule和scheduleAtFixedRate区别
- Time schedule和scheduleAtFixedRate区别
- Timer的schedule和scheduleAtFixedRate的区别
- Timer的schedule和scheduleAtFixedRate的区别
- Timer的schedule和scheduleAtFixedRate的区别
- java定时器 schedule和scheduleAtFixedRate区别
- Timer的scheduleAtFixedRate和schedule区别
- schedule、scheduleAtFixedRate和scheduleWithFixedDelay的区别
- schedule方法和scheduleAtFixedRate方法的区别
- scheduleAtFixedRate与schedule区别
- schedule和scheduleAtFixedRate
- Java Socket编程,一个完整的简单通信示例
- smarty安装和使用入门
- django查询数据库
- sqlite3中的数据类型
- 遇到一个面试题,用递归法实现字符串反转.
- schedule和scheduleAtFixedRate区别
- Android开发指南 1──应用程序基础
- decimal(numeric) 、float 、real 的区别
- 实用技巧:修改Linux操作系统的文件属性
- linux获取内核信息
- “安静的改变”究竟有多安静(算数转换)
- 多姿态人脸识别
- 读书
- 如何限制远程桌面中的磁盘映射和打印机映射!