java schedule参数_Java之Timer的schedule方法period参数分析

下面为一段标准的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执行完后,立即进行下一次

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值