下面为一段标准的Timer用法private void runTimer(){
Log.d("bloguan", "run:" + Thread.currentThread().getId());
new Timer().schedule(new TimerTask() {
@Override
public void run() {
Log.d("bloguan", "begin:" + Thread.currentThread().getId());
Log.d("bloguan", "end:" + Thread.currentThread().getId());
}
},1000, 5000);
}
执行结果11-06 11:25:04.105 2385-2385/com.bloguan.demo D/bloguan: run:1
11-06 11:25:05.106 2385-2410/com.bloguan.demo D/bloguan: begin:139
11-06 11:25:05.106 2385-2410/com.bloguan.demo D/bloguan: end:139
11-06 11:25:10.108 2385-2410/com.bloguan.demo D/bloguan: begin:139
11-06 11:25:10.108 2385-2410/com.bloguan.demo D/bloguan: end:139
分析:Timer延时1秒执行,每5秒循环一次,TimerTask的run运行在子线程
那么,有个疑问,如果run里面执行耗时操作,每5秒的循环周期还准吗?
试一下,我们在run中加一个2秒“耗时操作”private void runTimer(){
Log.d("bloguan", "run:" + Thread.currentThread().getId());
new Timer().schedule(new TimerTask() {
@Override
public void run() {
Log.d("bloguan", "begin:" + Thread.currentThread().getId());
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Log.d("bloguan", "end:" + Thread.currentThread().getId());
}
},1000, 5000);
}
执行结果11-06 11:30:27.947 2496-2496/com.bloguan.demo D/bloguan: run:1
11-06 11:30:28.948 2496-2514/com.bloguan.demo D/bloguan: begin:143
11-06 11:30:30.956 2496-2514/com.bloguan.demo D/bloguan: end:143
11-06 11:30:33.949 2496-2514/com.bloguan.demo D/bloguan: begin:143
11-06 11:30:35.949 2496-2514/com.bloguan.demo D/bloguan: end:143
11-06 11:30:38.951 2496-2514/com.bloguan.demo D/bloguan: begin:143
11-06 11:30:40.952 2496-2514/com.bloguan.demo D/bloguan: end:143
分析:定时器仍然每5秒触发一次,run里面的2秒耗时操作不会叠加到5秒这个周期上
如果耗时操作大于period这个周期呢?
把上面代码中的sleep改为6秒Thread.sleep(6000);
执行结果11-06 11:43:22.544 2586-2586/com.bloguan.demo D/bloguan: run:1
11-06 11:43:23.546 2586-2604/com.bloguan.demo D/bloguan: begin:147
11-06 11:43:29.546 2586-2604/com.bloguan.demo D/bloguan: end:147
11-06 11:43:29.546 2586-2604/com.bloguan.demo D/bloguan: begin:147
11-06 11:43:35.593 2586-2604/com.bloguan.demo D/bloguan: end:147
11-06 11:43:35.593 2586-2604/com.bloguan.demo D/bloguan: begin:147
分析:Timer的周期先到,但run没执行完,会等到执行完成后再次循环
结论:
1、TimerTask的run运行在子线程,非UI线程
2、如果TimerTask的run中有耗时操作
当耗时操作小于period周期时,仍以period时间为准进行周期循环
当耗时操作大于period周期时,等run执行完后,立即进行下一次