Java中的Timer和TimerTask在Android中的用法

在开发中我们有时会有这样的需求,即在固定的每隔一段时间执行某一个任务。比如UI上的控件需要随着时间改变,我们可以使用Java为我们提供的计时器的工具类,即Timer和TimerTask。 


Timer是一个普通的类,其中有几个重要的方法;而TimerTask则是一个抽象类,其中有一个抽象方法run(),类似线程中的run()方法,我们使用Timer创建一个他的对象,然后使用这对象的schedule方法来完成这种间隔的操作。


schedule方法有三个参数
第一个参数就是TimerTask类型的对象,我们实现TimerTask的run()方法就是要周期执行的一个任务;
第二个参数有两种类型,第一种是long类型,表示多长时间后开始执行,另一种是Date类型,表示从那个时间后开始执行;
第三个参数就是执行的周期,为long类型。


schedule方法还有一种两个参数的执行重载,第一个参数仍然是TimerTask,第二个表示为long的形式表示多长时间后执行一次,为Date就表示某个时间后执行一次。 


Timer就是一个线程,使用schedule方法完成对TimerTask的调度,多个TimerTask可以共用一个Timer,也就是说Timer对象调用一次schedule方法就是创建了一个线程,并且调用一次schedule后TimerTask是无限制的循环下去的,使用Timer的cancel()停止操作。当然同一个Timer执行一次cancel()方法后,所有Timer线程都被终止。






用法:




[java] view plain copy
//true 说明这个timer以daemon方式运行(优先级低,程序结束timer也自动结束)   
java.util.Timer timer = new java.util.Timer(true);  
  
TimerTask task = new TimerTask() {  
   public void run() {  
   //每次需要执行的代码放到这里面。     
   }     
};  
  
//以下是几种调度task的方法:  
  
//time为Date类型:在指定时间执行一次。  
timer.schedule(task, time);  
  
//firstTime为Date类型,period为long,表示从firstTime时刻开始,每隔period毫秒执行一次。  
timer.schedule(task, firstTime, period);     
  
//delay 为long类型:从现在起过delay毫秒执行一次。  
timer.schedule(task, delay);  
  
//delay为long,period为long:从现在起过delay毫秒以后,每隔period毫秒执行一次。  

timer.schedule(task, delay, period);  




以下例程序来测试上述结论,TimerTask需要执行6秒钟,但是间隔周期为5秒钟

[java]  view plain copy print ?
  1. package test;  
  2. import java.text.ParseException;  
  3. import java.text.SimpleDateFormat;  
  4. import java.util.Date;  
  5. import java.util.Timer;  
  6. import java.util.TimerTask;  
  7. public class Test {  
  8.       
  9.     public static void main(String[] args) throws ParseException {  
  10.         SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");  
  11.         Date startDate = dateFormatter.parse("2010/11/28 01:06:00");  
  12.         Timer timer = new Timer();  
  13.         timer.schedule(new TimerTask(){  
  14.            public void run() {  
  15.                try {  
  16.                    Thread.sleep(6000);  
  17.                } catch (InterruptedException e) {  
  18.                    e.printStackTrace();  
  19.                }  
  20.                System.out.println("execute task!"this.scheduledExecutionTime());  
  21.            }  
  22.         },startDate, 5 * 1000);  
  23.     }  
  24.       
  25. }  

schedule方法的执行结果如下:
execute task!1290877560001
execute task!1290877566001
execute task!1290877572001
execute task!1290877578001
execute task!1290877584001
execute task!1290877590001
execute task!1290877596001
execute task!1290877602001
execute task!1290877608001
execute task!1290877614001
execute task!1290877620001
execute task!1290877626001
execute task!1290877632001
execute task!1290877638001
可以看出,间隔时间都为6秒,因此, 下一次的执行时间点=上一次程序执行完成的时间点+间隔时间
当换成scheduleAtFixedRate方法的执行结果如下:
execute task!1290877860000
execute task!1290877865000
execute task!1290877870000
execute task!1290877875000
execute task!1290877880000
execute task!1290877885000
execute task!1290877890000
execute task!1290877895000
execute task!1290877900000
execute task!1290877905000
execute task!1290877910000
execute task!1290877915000
execute task!1290877920000
execute task!1290877925000
execute task!1290877930000
可以看出,间隔时间都为5秒,因此, 下一次的执行时间点=上一次程序开始执行的时间点+间隔时间 ;并且因为前一个任务要执行6秒,而当前任务已经开始执行了,因此两个任务间存在重叠,需要考虑线程同步

发布了2 篇原创文章 · 获赞 1 · 访问量 1480

猜你喜欢

转载自blog.csdn.net/qq_23934693/article/details/71172878