java多线程(二) - Timer 源码阅读

java多线程(二) - Timer 源码阅读

在JDK5ScheduledThreadPoolExecutor 之前。使用Timer定时器使用

Timer在调用shedulue TimerTask的时候,会把这个task放到一个队列里

  private final TaskQueue queue = new TaskQueue();

在Timer new出来的时候,会启动一个Thread
这个线程会循环的从queue中取Task出来执行, 一个一个执行;
·
每一个task会存一个应该执行的时间;

class TimerTask implements Runnable {
    ...
    long nextExecutionTime;
    ...
}
  1. 如果时间到了,就立即执行
  2. 如果时间没到,会整个timer wait
while(true) {
    ...
    task = queue.getMin();
    ...
    bool taskFired = (executionTime<=currentTime)

    if (!taskFired) // Task hasn't yet fired; wait
        queue.wait(executionTime - currentTime);
    else {
        task.run();
        ...
    }
    ...
}
shedule和scheduleAtFixedRate

当这个task执行完之后,设定下次一个要执行的task的时间上有区别;

  1. shedule 下次task的执行时间 = 这个task执行完的时间 + 间隔时间
  2. cheduleAtFixedRate 下次执行task的时间 = 这个task应该执行的时间 + 间隔时间
while(true) {
    ...
    executionTime = task.nextExecutionTime;

    // scheduleAtFixedRate 的period是用正数标示,schedule 则是用负数标示
    queue.rescheduleMin(task.period<0 ? currentTime   - task.period : executionTime + task.period);
    ...
}

也就是说:

  1. 如果一段时间内本应该执行很多task,但超时了,那么如果使用cheduleAtFixedRate,这短时间内本应该执行到次数最后还是会补上,并且会很快的补完
  2. 一个Timer不会同时执行
  3. Timer没有try机制,如果抛出了异常,就不会再执行后面的任务了·
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值