您的位置:首页 > 其它

关于TimerTask里面的scheduledExecutionTime()方法,一些见解

2017-12-20 17:36 567 查看
笔者为了测试TimerTask里面的scheduledExecutionTime()这个方法,编写了如下测试代码

public class MyTimerTest02 {
public static void main(String[] args) {
Timer timer=new Timer();
//		Calendar calendar=Calendar.getInstance();
Date date=new Date();
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-hh HH:mm:ss");
System.out.println(sdf.format(date));
//		calendar.add(Calendar.SECOND, 2);

//		myTimerTask02.setName("test TimerTask Cancel");
//
//		timer.schedule(myTimerTask02, 2000, 1000);

TimerTask03 myTimerTask03=new TimerTask03("near time");
timer.schedule(myTimerTask03,2000,1000);
System.out.println("Timer最近会执行的Task的时间:"+sdf.format(myTimerTask03.scheduledExecutionTime()));
}
}


public class TimerTask03 extends TimerTask{
private String name;
public TimerTask03(String name)
{
this.name=name;
}
@Override
public void run() {
// TODO Auto-generated method stub
//		Calendar calendar=Calendar.getInstance();
System.out.println("------------>");
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-hh HH:mm:ss");
//		System.out.println("Timer最近会执行的Task的时间:"+sdf.format(scheduledExecutionTime()));
System.out.println("This TimerTask's time is "+sdf.format(new Date()));
System.out.println("This TimerTask's name is "+name);
System.out.println("<------------");
System.out.println();

}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}

}


执行结果截取

2017-12-10 22:00:32
Timer最近会执行的Task的时间:2017-12-10 22:00:33
------------>
This TimerTask's time is 2017-12-10 22:00:34
This TimerTask's name is near time
<------------

------------>
This TimerTask's time is 2017-12-10 22:00:35
This TimerTask's name is near time
<------------


由此看来当你在测试类下面直接调用scheduledExecutionTime()预计时间会出现偏差

但是如果你把

timer.schedule(myTimerTask03,2000,1000);


改成如下

timer.schedule(myTimerTask03,2000);


结果就是

2017-12-10 22:04:33
Timer最近会执行的Task的时间:2017-12-10 22:04:35
------------>
This TimerTask's time is 2017-12-10 22:04:35
This TimerTask's name is near time
<------------


小结:如果把scheduledExecutionTime()放在测试类使用测试出来的预计执行时间结果分两种情况

1、timer使用两个参数的schedule是准确的

2、timer使用三个参数的schedule是会比实际执行情况少一分钟

如果我把测试类改成这样

public class MyTimerTest02 {
public static void main(String[] args) {
Timer timer=new Timer();
//		Calendar calendar=Calendar.getInstance();
Date date=new Date();
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-hh HH:mm:ss");
System.out.println(sdf.format(date));
//		calendar.add(Calendar.SECOND, 2);

//		myTimerTask02.setName("test TimerTask Cancel");
//
//		timer.schedule(myTimerTask02, 2000, 1000);

TimerTask03 myTimerTask03=new TimerTask03("near time");
timer.schedule(myTimerTask03,2000,1000);
//		System.out.println("Timer最近会执行的Task的时间:"+sdf.format(myTimerTask03.scheduledExecutionTime()));
}
}


把TimerTask03改成

public class TimerTask03 extends TimerTask{
private String name;
public TimerTask03(String name)
{
this.name=name;
}
@Override
public void run() {
// TODO Auto-generated method stub
//		Calendar calendar=Calendar.getInstance();
System.out.println("------------>");
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-hh HH:mm:ss");
System.out.println("Timer最近会执行的Task的时间:"+sdf.format(scheduledExecutionTime()));
System.out.println("This TimerTask's time is "+sdf.format(new Date()));
System.out.println("This TimerTask's name is "+name);
System.out.println("<------------");
System.out.println();

}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}

}


结果是

2017-12-10 22:07:25
------------>
Timer最近会执行的Task的时间:2017-12-10 22:07:28
This TimerTask's time is 2017-12-10 22:07:28
This TimerTask's name is near time
<------------

------------>
Timer最近会执行的Task的时间:2017-12-10 22:07:29
This TimerTask's time is 2017-12-10 22:07:29
This TimerTask's name is near time
<------------



4000
结:在Task类里面调用scheduledExecutionTime()

因为是预计执行时间,取的是最近注意是最近,在Task里面调用之后,时间就是当前调用Task的时间。

总结:这个scheduledExecutionTime()还是有略微的问题的,我就不分析底层了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐