一篇文章带你详解定时器 Timer 的运行机制

本篇博文基于jdk1.8对Timer定时器进行深入分析,带你详解Timer定时器的原理

package java.util;
import java.util.Date;
import java.util.concurrent.atomic.AtomicInteger;

public class Timer {
  
    private final TaskQueue queue = new TaskQueue(); //维持的任务队列

    /**
     * The timer thread.
     */
    private final TimerThread thread = new TimerThread(queue); //执行任务队列的线程

    private final Object threadReaper = new Object() {
        protected void finalize() throws Throwable {
            synchronized(queue) {
                thread.newTasksMayBeScheduled = false;
                queue.notify(); // In case queue is empty.
            }
        }
    };

    /**
     * This ID is used to generate thread names.用于生成线程的名字
     */
    private final static AtomicInteger nextSerialNumber = new AtomicInteger(0);
    private static int serialNumber() {
        return nextSerialNumber.getAndIncrement();
    }

    /**
     * Creates a new timer.  The associated thread does <i>not</i>
     * {@linkplain Thread#setDaemon run as a daemon}.
     */
    public Timer() {
        this("Timer-" + serialNumber()); //初始化一个任务队列,并设置线程名
    }

   
    public Timer(boolean isDaemon) {
        this("Timer-" + serialNumber(), isDaemon); //创建一个timer并指定 是否是守护线程 isDaemon为true代表是,否代表不是
    }

 
    public Timer(String name) { //
        thread.setName(name);
        thread.start();
    }

    /**
     * Creates a new timer whose associated thread has the specified name,
     * and may be specified to
     * {@linkplain Thread#setDaemon run as a daemon}.
     *
     * @param name the name of the associated thread
     * @param isDaemon true if the associated thread should run as a daemon
     * @throws NullPointerException if {@code name} is null
     * @since 1.5
     */
    public Timer(String name, boolean isDaemon) {
        thread.setName(name);
        thread.setDaemon(isDaemon);
        thread.start();
    }

   
    public void schedule(TimerTask task, long delay) { //指定多长时间后调度任务,
        if (delay < 0)
            throw new IllegalArgumentException("Negative delay.");
        sched(task, System.currentTimeMillis()+delay, 0); //具体的调用方法,为0表示调度一次
    }

   
    public void schedule(TimerTask task, Date time) { //指定固定的日期调度任务,如果日期为过去的日期如2017年 则会立即调度
        sched(task, time.getTime(), 0);
    }

    public void schedule(TimerTask task, long delay, long period) { //指定时间后开始调用该任务,并且是周期性的调用
        if (delay < 0)
            throw new IllegalArgumentException("Negative delay.");
        if (period <= 0)
            throw new IllegalArgumentException("Non-positive period.");
        sched(task, System.currentTimeMillis()+delay, -period);
    }

    public void schedule(TimerTask task, Date firstTime, long period) {   指定第一个执行的时间,并指定执行周期
        if (period <= 0)
            throw new IllegalArgumentException("Non-positive period.");
        sched(task, firstTime.getTime(), -period);
    }

   
//    指定延迟时间后,周期执行任务,绝对时间,在文章末尾会详解与schedule的区别
    public void scheduleAtFixedRate(TimerTask task, long delay, long period) {
        if (delay < 0)
            throw new IllegalArgumentException("Negative delay.");
        if (period <= 0)
            throw new IllegalArgumentException("Non-positive period.");
        sched(task, System.currentTimeMillis()+delay, period);
    }

    
    public void scheduleAtFixedRate(TimerTask task, Date firstTime,
                                    long period) {
        if (period <= 0)
            throw new IllegalArgumentException("Non-positive period.");
        sched(task, firstTime.getTime(), period);
    }

    
    //具体的调度算法
    private void sched(TimerTask task, long time, long period) {
        if (time < 0)
            throw new IllegalArgumentException("Illegal execution time.");

        // Constrain value of period sufficiently to prevent numeric
        // overflow while still being effectively infinitely large.

      //如果period过大,右移一位,主要考虑到执行时间过长,有点不切实际
        if (Math.abs(period) > (Long.MAX_VALUE >> 1))
            period >>= 1;

        synchronized(queue) {  //具体的任务调度,需要获取queue锁
            if (!thread.newTasksMayBeScheduled)
                throw new IllegalStateException("Timer already cancelled.");

            synchronized(task.lock) {   //锁住任务,因为任务对象是共享的,放置任务的线程也可以操作该task,改变其状态
                if (task.state != TimerTask.VIRGIN)  //任务 状态VIRGIN 为初始状态,表示任务没有被调度
                    throw new IllegalStateException(
                        "Task already scheduled or cancelled");
                task.nextExecutionTime = time;
                task.period = period; //任务的执行周期
                task.state = TimerTask.SCHEDULED; //设置任务的状态 SCHEDULED表示正计划被执行(但不一定会马上被执行)
            }

            queue.add(task); //加入任务队列
            if (queue.getMin() == task) //新加入的任务为与队头,为最先执行
                queue.notify(); //重新唤醒任务线程,需要重新调整等待时间,使得原先线程等待时间为最先执行任务的等待时间
        }
    }

    
    
    public void cancel() { //取消任务队列
        synchronized(queue) {
            thread.newTasksMayBeScheduled = false;
            queue.clear();
            queue.notify();  // In case queue was already empty.//唤醒在queue上等待的线程
        }
    }
    
 //移除取消的任务,以时间换空间的策略,很少使用
     public int purge() {
         int result = 0;

         synchronized(queue) {
             for (int i = queue.size(); i > 0; i--) {
                 if (queue.get(i).state == TimerTask.CANCELLED) {
                     queue.quickRemove(i);
                     result++;
                 }
             }

             if (result != 0)
                 queue.heapify();
         }

         return result;
     }
}


 //调度任务执行的线程
class TimerThread extends Thread {
 
  //为ture时代表该线程可以调度任务队伍中的任务,为false则不行,
  //该属性被queue监视锁保护,也就是,规定需要获取queue监视锁才能操作该值
    boolean newTasksMayBeScheduled = true;

   //任务队列
    private TaskQueue queue;

    TimerThread(TaskQueue queue) {
        this.queue = queue;
    }
    //

    public void run() {
        try {
            mainLoop(); //主要运行方法
        } finally {
            // Someone killed this Thread, behave as if Timer cancelled
            synchronized(queue) { //这里主要是因为线程被杀死,正常情况下不会执行到这里
                newTasksMayBeScheduled = false;
                queue.clear();  // Eliminate obsolete references
            }
        }
    }

    /**
     * The main timer loop.  (See class comment.)
     */
    private void mainLoop() {
        while (true) {
            try {
                TimerTask task; //调度的任务
                boolean taskFired;
                synchronized(queue) { 
                    // Wait for queue to become non-empty
                    while (queue.isEmpty() && newTasksMayBeScheduled) //为空且newTasksMayBeScheduled,调用wait方法等待
                        queue.wait();
                    if (queue.isEmpty())
                        break; // Queue is empty and will forever remain; die 

                    // Queue nonempty; look at first evt and do the right thing
                    long currentTime, executionTime;
                    task = queue.getMin();

                  //解释一下这里为什么加锁,任务由别的线程加入,因此,状态别的线程可以改变,如设置取消任务
                    synchronized(task.lock) {
                        if (task.state == TimerTask.CANCELLED) {
                            queue.removeMin();
                            continue;  // No action required, poll queue again
                        }
                        currentTime = System.currentTimeMillis(); //获取当前时间
                        executionTime = task.nextExecutionTime;  //获取任务预期执行时间
                        if (taskFired = (executionTime<=currentTime)) { //currentTime>=executionTime 
                            if (task.period == 0) { //period为0 代表不是重复执行任务
                                queue.removeMin();
                                task.state = TimerTask.EXECUTED; //设置任务的状态:EXECUTED表示调度完成
                            } else { // Repeating task, reschedule
                                queue.rescheduleMin( //重新构建任务
                                  task.period<0 ? currentTime   - task.period     对应schedule调度方式
                                                : executionTime + task.period);   对应 scheduleAtFixedRate 调用方式
                            }
                        }
                    }
                    if (!taskFired) // Task hasn't yet fired; wait //为ture等待
                        queue.wait(executionTime - currentTime);
                }
                if (taskFired)  // Task fired; run it, holding no locks
                task.run();  //执行定时任务 ,想像一下这里如果抛出的不是InterruptedException,而是如NullpointerExcetion会怎样?
            } catch(InterruptedException e) {
            }
        }
    }
}


//任务队里,维持一个任务数组
//里面按照nextExecutionTime用最小堆的方式对任务进行排序
//所以不会最小堆的请自行百度堆排序,这里不多讲
//taskQueue中,下标为0的位置保留,以下标为1的结点做根元素,以方便使用位运算
class TaskQueue {
   
    private TimerTask[] queue = new TimerTask[128];

    /**
     * The number of tasks in the priority queue.  (The tasks are stored in
     * queue[1] up to queue[size]).
     */
    private int size = 0;

    /**
     * Returns the number of tasks currently on the queue.
     */
    int size() {
        return size;
    }

    /**
     * Adds a new task to the priority queue.
     */
     //添加任务
    void add(TimerTask task) {
        // Grow backing store if necessary
        if (size + 1 == queue.length)
            queue = Arrays.copyOf(queue, 2*queue.length);

        queue[++size] = task;
        fixUp(size); //重新调整堆
    }
//返回任务
    TimerTask getMin() {
        return queue[1];
    }

  
  //返回堆顶元素
    TimerTask get(int i) {
        return queue[i];
    }

    //移除堆顶元素
    void removeMin() {
        queue[1] = queue[size];
        queue[size--] = null;  // Drop extra reference to prevent memory leak
        fixDown(1); //重新调整堆
    }

    /**
     * Removes the ith element from queue without regard for maintaining
     * the heap invariant.  Recall that queue is one-based, so
     * 1 <= i <= size.
     */
    void quickRemove(int i) {
        assert i <= size;

        queue[i] = queue[size];
        queue[size--] = null;  // Drop extra ref to prevent memory leak
    }

    /**
     * Sets the nextExecutionTime associated with the head task to the
     * specified value, and adjusts priority queue accordingly.
     */
    void rescheduleMin(long newTime) {
        queue[1].nextExecutionTime = newTime;
        fixDown(1);
    }

    /**
     * Returns true if the priority queue contains no elements.
     */
    boolean isEmpty() {
        return size==0;
    }

    /**
     * Removes all elements from the priority queue.
     */
    void clear() {
        // Null out task references to prevent memory leak
        for (int i=1; i<=size; i++)
            queue[i] = null;

        size = 0;
    }

    
    
    private void fixUp(int k) {
        while (k > 1) {
            int j = k >> 1;
            if (queue[j].nextExecutionTime <= queue[k].nextExecutionTime)
                break;
            TimerTask tmp = queue[j];  queue[j] = queue[k]; queue[k] = tmp;
            k = j;
        }
    }

  
    private void fixDown(int k) {
        int j;
        while ((j = k << 1) <= size && j > 0) {
            if (j < size &&
                queue[j].nextExecutionTime > queue[j+1].nextExecutionTime)
                j++; // j indexes smallest kid
            if (queue[k].nextExecutionTime <= queue[j].nextExecutionTime)
                break;
            TimerTask tmp = queue[j];  queue[j] = queue[k]; queue[k] = tmp;
            k = j;
        }
    }

   
    void heapify() {
        for (int i = size/2; i >= 1; i--)
            fixDown(i);
    }
}
 

最后许多同学对schedule和scheduleAtFixedRate间的差别不了解,这里我画了图和举个例子帮助大家理解,

 

schedule:假设使用schedule调用任务task,由于某种原因(如垃圾收集等)造成某周期执行的任务原本在t1段执行的任务延长到t2段执行,在计算下一次任务(按照原先如果没延迟本应在t3执行)执行时间,会从当前执行任务的时间开始计算,得到下一次计算执行时间在t4段执行,而如果使用scheduleAtFixedRate,则不同下一次执行时间仍然为t3。 打个比方小明有一箱苹果,打算每隔5分中就得一个苹果,假设从8:00开始吃,在8:05分小明吃了第一个苹果,但在下次吃苹果的途中小明想打扫房间,打扫完后发现时间为8:12了,如果按照schedule调用,小明现在吃了后下次吃苹果的时间就变为8:17分了,而按照scheduleAtFixedRate则下次吃苹果的时间与原先计划的一致,还是为8:15分。

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值