书上讲解时,有点小问题,网上讲解很乱,下面自己实现一下区别
使用工具:IntelliJ IDEA
1.当TimerTask中执行时长小于给定周期period时,且给定执行TimerTask的时间晚于当前时间 (即正常情况下)
schedule()的情况
public class Run3 {
private static Timer timer = new Timer();
private static int runCount = 0;
static public class MyTask1 extends TimerTask {
@Override
public void run() {
try {
System.out.println("1 begin 运行了! " + new Date());
Thread.sleep(3000);
System.out.println("1 end 结束了" + new Date());
runCount++;
if (runCount >= 5) {
timer.cancel();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
try {
MyTask1 task1=new MyTask1();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println("time: " + sdf.format(new Date()));
String dateString = "2017-3-06 14:42:00";
Date date = sdf.parse(dateString);
timer.schedule(task1,date,5000);
} catch (ParseException e) {
e.printStackTrace();
}
}
}
【此为完整代码,下面只是复制关键代码】
运行结果
time: 2017-03-06 14:41:28 -------------------------------------------------------->当前时间
1 begin 运行了! Mon Mar 06 14:42:00 CST 2017---------------------------->执行时间
1 end 结束了Mon Mar 06 14:42:03 CST 2017---------------------------------->执行完成时间,执行时长3秒
1 begin 运行了! Mon Mar 06 14:42:05 CST 2017---------------------------->下一次执行时间,周期5秒
1 end 结束了Mon Mar 06 14:42:08 CST 2017
1 begin 运行了! Mon Mar 06 14:42:10 CST 2017
1 end 结束了Mon Mar 06 14:42:13 CST 2017
1 begin 运行了! Mon Mar 06 14:42:15 CST 2017
1 end 结束了Mon Mar 06 14:42:18 CST 2017
1 begin 运行了! Mon Mar 06 14:42:20 CST 2017
1 end 结束了Mon Mar 06 14:42:23 CST 2017
Process finished with exit code 0
scheduleAtFixedRate()的情况
将上面关键行的改为
timer.scheduleAtFixedRate(task1,date,5000);
运行结果
time: 2017-03-06 14:53:15
1 begin 运行了! Mon Mar 06 14:54:00 CST 2017
1 end 结束了Mon Mar 06 14:54:03 CST 2017
1 begin 运行了! Mon Mar 06 14:54:05 CST 2017
1 end 结束了Mon Mar 06 14:54:08 CST 2017
1 begin 运行了! Mon Mar 06 14:54:10 CST 2017
1 end 结束了Mon Mar 06 14:54:13 CST 2017
1 begin 运行了! Mon Mar 06 14:54:15 CST 2017
1 end 结束了Mon Mar 06 14:54:18 CST 2017
1 begin 运行了! Mon Mar 06 14:54:20 CST 2017
1 end 结束了Mon Mar 06 14:54:23 CST 2017
Process finished with exit code 0
结果:此条件下两种方法的结果相同,均是在给定的周期为5下循环执行
2.当TimerTask中执行时长大于给定周期period时,而给定执行TimerTask的时间晚于当前时间
schedule()的情况
MyTask1 task1=new MyTask1();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println("time: " + sdf.format(new Date()));
String dateString = "2017-3-06 14:59:00";
Date date = sdf.parse(dateString);
timer.schedule(task1,date,2000);
运行结果
time: 2017-03-06 14:58:38---------------------------------------------------------->当前时间
1 begin 运行了! Mon Mar 06 14:59:00 CST 2017----------------------------->执行时间
1 end 结束了Mon Mar 06 14:59:03 CST 2017----------------------------------->执行完成时间,执行时长3秒
1 begin 运行了! Mon Mar 06 14:59:03 CST 2017----------------------------->下次执行时间,周期3秒
1 end 结束了Mon Mar 06 14:59:06 CST 2017
1 begin 运行了! Mon Mar 06 14:59:06 CST 2017
1 end 结束了Mon Mar 06 14:59:09 CST 2017
1 begin 运行了! Mon Mar 06 14:59:09 CST 2017
1 end 结束了Mon Mar 06 14:59:12 CST 2017
1 begin 运行了! Mon Mar 06 14:59:12 CST 2017
1 end 结束了Mon Mar 06 14:59:15 CST 2017
Process finished with exit code 0
scheduleAtFixedRate()的情况
MyTask1 task1=new MyTask1();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println("time: " + sdf.format(new Date()));
String dateString = "2017-3-06 15:05:00";
Date date = sdf.parse(dateString);
timer.scheduleAtFixedRate(task1,date,2000);
运行结果
time: 2017-03-06 15:04:18------------------------------------------------------->当前时间
1 begin 运行了! Mon Mar 06 15:05:00 CST 2017-------------------------->执行时间
1 end 结束了Mon Mar 06 15:05:03 CST 2017-------------------------------->执行结束时间,执行时长3秒
1 begin 运行了! Mon Mar 06 15:05:03 CST 2017-------------------------->下一次开始时间,周期3秒
1 end 结束了Mon Mar 06 15:05:06 CST 2017
1 begin 运行了! Mon Mar 06 15:05:06 CST 2017
1 end 结束了Mon Mar 06 15:05:09 CST 2017
1 begin 运行了! Mon Mar 06 15:05:09 CST 2017
1 end 结束了Mon Mar 06 15:05:12 CST 2017
1 begin 运行了! Mon Mar 06 15:05:12 CST 2017
1 end 结束了Mon Mar 06 15:05:15 CST 2017
Process finished with exit code 0
结果:当执行TimerTask的时长大于周期时,任务执行的周期改为执行任务的时长,两种方法没有区别
3.当TimerTask中执行时长小于给定周期period时,而给定执行TimerTask的时间早于当前时间
schedule()情况
MyTask1 task1=new MyTask1();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println("time: " + sdf.format(new Date()));
String dateString = "2017-3-06 15:05:00";
Date date = sdf.parse(dateString);
timer.schedule(task1,date,5000);
time: 2017-03-06 15:11:00----------------------------------------------->当前时间
1 begin 运行了! Mon Mar 06 15:11:00 CST 2017------------------>执行时间
1 end 结束了Mon Mar 06 15:11:03 CST 2017------------------------>执行完成时间,时长3秒
1 begin 运行了! Mon Mar 06 15:11:05 CST 2017------------------>下一次执行时间,周期5秒
1 end 结束了Mon Mar 06 15:11:08 CST 2017
1 begin 运行了! Mon Mar 06 15:11:10 CST 2017
1 end 结束了Mon Mar 06 15:11:13 CST 2017
1 begin 运行了! Mon Mar 06 15:11:15 CST 2017
1 end 结束了Mon Mar 06 15:11:18 CST 2017
1 begin 运行了! Mon Mar 06 15:11:20 CST 2017
1 end 结束了Mon Mar 06 15:11:23 CST 2017
Process finished with exit code 0
scheduleAtFixedRate()情况
MyTask1 task1=new MyTask1();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println("time: " + sdf.format(new Date()));
String dateString = "2017-3-06 15:05:00";
Date date = sdf.parse(dateString);
timer.scheduleAtFixedRate(task1,date,5000);
运行结果
time: 2017-03-06 15:09:28------------------------------------------------->当前时间
1 begin 运行了! Mon Mar 06 15:09:28 CST 2017-------------------->执行时间
1 end 结束了Mon Mar 06 15:09:31 CST 2017-------------------------->执行完成时间,时长3秒
1 begin 运行了! Mon Mar 06 15:09:31 CST 2017-------------------->下一次执行时间,周期3秒
1 end 结束了Mon Mar 06 15:09:34 CST 2017
1 begin 运行了! Mon Mar 06 15:09:34 CST 2017
1 end 结束了Mon Mar 06 15:09:37 CST 2017
1 begin 运行了! Mon Mar 06 15:09:37 CST 2017
1 end 结束了Mon Mar 06 15:09:40 CST 2017
1 begin 运行了! Mon Mar 06 15:09:40 CST 2017
1 end 结束了Mon Mar 06 15:09:43 CST 2017
Process finished with exit code 0
结果:此情况下,两种方法的调用出现了区别,schedule()方法的实际周期仍然是给定周期,而scheduleAtFixedRate()方法的实际周期则为执行时长
4.当TimerTask中执行时长大于给定周期period时,而给定执行TimerTask的时间早于当前时间
schedule()情况
MyTask1 task1=new MyTask1();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println("time: " + sdf.format(new Date()));
String dateString = "2017-3-06 15:05:00";
Date date = sdf.parse(dateString);
timer.schedule(task1,date,2000);
运行结果
time: 2017-03-06 15:12:24---------------------------------------------------->当前时间
1 begin 运行了! Mon Mar 06 15:12:24 CST 2017----------------------->执行时间
1 end 结束了Mon Mar 06 15:12:27 CST 2017----------------------------->执行完成时间,时长3秒
1 begin 运行了! Mon Mar 06 15:12:27 CST 2017---------------------->下一次执行时间,周期3秒
1 end 结束了Mon Mar 06 15:12:30 CST 2017
1 begin 运行了! Mon Mar 06 15:12:30 CST 2017
1 end 结束了Mon Mar 06 15:12:33 CST 2017
1 begin 运行了! Mon Mar 06 15:12:33 CST 2017
1 end 结束了Mon Mar 06 15:12:36 CST 2017
1 begin 运行了! Mon Mar 06 15:12:36 CST 2017
1 end 结束了Mon Mar 06 15:12:39 CST 2017
Process finished with exit code 0
scheduleAtFixedRate()情况
MyTask1 task1=new MyTask1();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println("time: " + sdf.format(new Date()));
String dateString = "2017-3-06 15:05:00";
Date date = sdf.parse(dateString);
timer.scheduleAtFixedRate(task1,date,2000);
运行结果
time: 2017-03-06 15:13:19------------------------------------------------>当前时间
1 begin 运行了! Mon Mar 06 15:13:19 CST 2017------------------->执行时间
1 end 结束了Mon Mar 06 15:13:22 CST 2017------------------------->执行完成时间,时长3秒
1 begin 运行了! Mon Mar 06 15:13:22 CST 2017------------------->下一次执行时间,周期3秒
1 end 结束了Mon Mar 06 15:13:25 CST 2017
1 begin 运行了! Mon Mar 06 15:13:25 CST 2017
1 end 结束了Mon Mar 06 15:13:28 CST 2017
1 begin 运行了! Mon Mar 06 15:13:28 CST 2017
1 end 结束了Mon Mar 06 15:13:31 CST 2017
1 begin 运行了! Mon Mar 06 15:13:31 CST 2017
1 end 结束了Mon Mar 06 15:13:34 CST 2017
Process finished with exit code 0
结果 : 此情况下没有区别
5.结论
当执行时长大于周期时,以时长为周期
当给定的执行时间早于当前时间时,schedule()方法的周期为给定的周期,而scheduleAtFixedRate()方法的周期为执行的时长