JDK Timer 源码

165 阅读11分钟

JDK Timer 源码

源码从上到下分析

// 存储定时任务执行的任务
private final TaskQueue queue = new TaskQueue();

/**
 * The timer thread. 执行的线程 只有一个线程 
 */
private final TimerThread thread = new TimerThread(queue);

Timer 构造器
public Timer(String name) {
 thread.setName(name);
 thread.start();
}
schedule 定时任务方法1
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);
}
sched 定时任务方法2
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.
    if (Math.abs(period) > (Long.MAX_VALUE >> 1))
        period >>= 1;
​
    synchronized(queue) {
        if (!thread.newTasksMayBeScheduled)
            throw new IllegalStateException("Timer already cancelled.");
​
        synchronized(task.lock) {
            if (task.state != TimerTask.VIRGIN)
                throw new IllegalStateException(
                    "Task already scheduled or cancelled");
            // 下次任务执行的时间戳 等于当前时间+传入的delay 参数
            task.nextExecutionTime = time;
            // 一段时间等于 -period
            task.period = period;
            // 任务的状态 为啥要这个字段 
            //  英文翻译结果 : 此任务计划执行。如果是非重复任务,则尚未执行
            task.state = TimerTask.SCHEDULED;
        }
        // 添加任务到队列中
        queue.add(task);
        if (queue.getMin() == task)
            queue.notify();
    }
}
TimerThread 线程核心类
class TimerThread extends Thread {
    /**
     * This flag is set to false by the reaper to inform us that there
     * are no more live references to our Timer object.  Once this flag
     * is true and there are no more tasks in our queue, there is no
     * work left for us to do, so we terminate gracefully.  Note that
     * this field is protected by queue's monitor!
     */
    boolean newTasksMayBeScheduled = true;

    /**
     * Our Timer's queue.  We store this reference in preference to
     * a reference to the Timer so the reference graph remains acyclic.
     * Otherwise, the Timer would never be garbage-collected and this
     * thread would never go away.
     */
    private TaskQueue queue;

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

    // 开启线程执行的任务  Timer timer =  new Timer (); 
    // 会启动线程 因为 thred.start(); 设计巧妙之处
    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;
                //  到任务时间点了 taskFired=true 没到taskFired=false
                boolean taskFired;
                synchronized(queue) 
                {
                    // Wait for queue to become non-empty
                    while (queue.isEmpty() && 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)) 
                        {
                            if (task.period == 0) { // Non-repeating, remove
                                // period =0 不是周期性任务  删除数组第一个元素 修改任务状态 执行过了
                                queue.removeMin();
                                // 英文翻译 : 此非重复任务已执行(或当前正在执行)且尚未取消。 
                                task.state = TimerTask.EXECUTED;
                            } else { // Repeating task, reschedule
                                
                                // 固定频率的执行的话 scheduleAtFixedRate 下次执行的时间是上次任务的执行时间+时间间隔 
                                // 
                                queue.rescheduleMin( task.period<0 ? currentTime   - task.period: executionTime + task.period);
                                 
                                                
                            }
                        }
                    }
                    // 发下任务没到执行时间的时候 线程等待executionTime - currentTime 时间段后开启执行任务
                    if (!taskFired) // Task hasn't yet fired; wait
                        queue.wait(executionTime - currentTime);
                }
                if (taskFired)  // Task fired; run it, holding no locks
                    // 执行任务run 方法
                    task.run();
            } 
            catch(InterruptedException e) 
            {
                
            }
        }
    }
}
TaskQueue 最小堆 =小根堆 优先级队列 最小堆数据结构的算法
class TaskQueue {
    /**
     * Priority queue represented as a balanced binary heap: the two children
     * of queue[n] are queue[2*n] and queue[2*n+1].  The priority queue is
     * ordered on the nextExecutionTime field: The TimerTask with the lowest
     * nextExecutionTime is in queue[1] (assuming the queue is nonempty).  For
     * each node n in the heap, and each descendant of n, d,
     * n.nextExecutionTime <= d.nextExecutionTime.
     */
    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);
    }

    /**
     * Return the "head task" of the priority queue.  (The head task is an
     * task with the lowest nextExecutionTime.)
     */
    TimerTask getMin() {
        return queue[1];
    }

    /**
     * Return the ith task in the priority queue, where i ranges from 1 (the
     * head task, which is returned by getMin) to the number of tasks on the
     * queue, inclusive.
     */
    TimerTask get(int i) {
        return queue[i];
    }

    /**
     * Remove the head task from the priority queue.
     */
    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;
    }

    /**
     * Establishes the heap invariant (described above) assuming the heap
     * satisfies the invariant except possibly for the leaf-node indexed by k
     * (which may have a nextExecutionTime less than its parent's).
     *
     * This method functions by "promoting" queue[k] up the hierarchy
     * (by swapping it with its parent) repeatedly until queue[k]'s
     * nextExecutionTime is greater than or equal to that of its parent.
     */
    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;
        }
    }

    /**
     * Establishes the heap invariant (described above) in the subtree
     * rooted at k, which is assumed to satisfy the heap invariant except
     * possibly for node k itself (which may have a nextExecutionTime greater
     * than its children's).
     *
     * This method functions by "demoting" queue[k] down the hierarchy
     * (by swapping it with its smaller child) repeatedly until queue[k]'s
     * nextExecutionTime is less than or equal to those of its children.
     */
    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;
        }
    }

    /**
     * Establishes the heap invariant (described above) in the entire tree,
     * assuming nothing about the order of the elements prior to the call.
     */
    void heapify() {
        for (int i = size/2; i >= 1; i--)
            fixDown(i);
    }
}
再次解读

在Java中,很常见的一个定时器的实现就是 Timer 类,用来实现定时、延迟执行、周期性执行任务的功能。

Timer 是定义在 java.util 中的一个工具类,提供简单的实现定时器的功能。和它配合使用的,是 TimerTask 类,这是对一个可以被调度的任务的封装。使用起来非常简单,如下示例:

// 0. 定义一个可调度的任务,继承自 TimerTask class FooTimerTask extends TimerTask {

@Override
public void run() {
    // do your things
}

}

// 1. 初始化Timer 定时器对象 Timer timer = new Timer("barTimer");

// 2. 初始化需要被调度的任务对象 TimerTask task = new FooTimerTask();

// 3. 调度任务。延迟1000毫秒后执行,之后每2000毫秒定时执行一次 timer.schedule(task, 1000, 2000);

以上,就是一个简单的使用Timer 的示例,下文将会分析Timer的源码实现。

  1. 概述

在Timer 机制中,涉及到的关键类如下:

Timer: 主要的调用的,提供对外的API;TimerTask: 是一个抽象类,定义一个任务,继承自RunnableTimerThread: 继承自 Thread,是一个自定义的线程类;TaskQueue: 一个任务队列,包含有当前Timer的所有任务,内部使用二叉堆来实现。

以上几个关键类的引用关系如下:

简要描述的话,是:

1个 TimerThread ---> 实现1个 线程

1个 Timer对象 ---> 持有1个 TimerThread 对象

1个 Timer对象 ---> 持有1个 TimerQueue 对象

1个 TimerQueue 对象 ---> 持有 n个 TimerTask 对象 2. 源码分析 2.1 Timer类的源码分析

源码分析的话,我们最好是按照Timer 的使用流程来分析。 首先,是Timer 的创建:

// Timer有四个构造方法,但是本质上其实是做的相同的事情,即 // 1. 使用name 和 isDeamon 两个参数给 thread 对象做了参数设置; // 2. 调用 thread 的 start() 方法启动线程 public Timer() { this("Timer-" + serialNumber()); }

public Timer(boolean isDaemon) { this("Timer-" + serialNumber(), isDaemon); }

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

public Timer(String name, boolean isDaemon) { thread.setName(name); thread.setDaemon(isDaemon); thread.start(); }

那么,或许大家会有一个疑问,thread 成员的初始化呢?这个时候,在代码里面找,就能发现:

// 这两个成员都是直接在声明的时候进行了初始化。 private final TaskQueue queue = new TaskQueue(); private final TimerThread thread = new TimerThread(queue);

可以看到 thread 和 queue两个成员都是在声明的时候直接初始化的,并且有意思的是,两个成员都是 final 类型的,这也就意味着这两个成员一旦创建就不会再改了,等于说把 thread、queue 和 Timer 对象这三者的生命周期强行绑定在一起了,大家一起创建,并且一经创建将会无法改变。

然后,创建了Timer 后,与之相关的队列也已经创建成功,而且相关联的线程也启动了,就可以进行任务的调度了,我们看下它的任务调度方法:

// Timer 包含有一组重载方法,参数为以下几个: // 1. TimerTask task:需要被调度的任务 // 2. long delay: 指定延迟的时间; // 3. long period: 指定调度的执行周期; schedule(TimerTask task, long delay, long period)

多个重载的调度方法在经过一些一些列的状态判断、参数设置、以及把delay时间转换成实际的执行时间等之后, 最终完成该功能的是 sched 方法,详情见注释部分:

这里涉及到一个需要留意的点,是在调用schedule 方法的时候,会根据TimerTask 的类型来进行不同的计算,进而给TimerTask设置不同的 period 参数,TimerTask 的类型有以下几种:

非周期性任务;对应 TimerTask.period 值为0;周期性任务,但是没有delay值,即立即执行;对应 TimerTask.period 值为正数;周期性任务,同时包含有 delay值;对应 TimerTask.period 值为负数;

在schedule 方法中,会

// 执行任务调度的方法 // 这里的 time 已经是经过转换的,表示该task 需要被执行的时间戳 private void sched(TimerTask task, long time, long period) { // 参数的合法性检查 if (time < 0) throw new IllegalArgumentException("Illegal execution time.");

if (Math.abs(period) > (Long.MAX_VALUE >> 1))
    period >>= 1;

// 核心的调度逻辑
// 由于是在多线程环境中使用的,这里为了保证线程安全,使用的是 synchronized 代码段
// 对象锁使用的是在 Timer 对象中唯一存在的 queue 对象
synchronized(queue) {

    // thread.newTasksMayBeScheduled 是一个标识位,在timer cancel之后 或者 thread 被停止后该标识位会被设为false
    // newTasksMayBeScheduled 为false 则表示该timer 的关联线程已经停止了。
    if (!thread.newTasksMayBeScheduled)
        throw new IllegalStateException("Timer already cancelled.");

    // 这里是把外部的参数,如执行时间点、执行周期、设置状态等等。
    // 这里为了线程安全的考虑,使用对 task 内部的 lock 对象加锁来保证。
    synchronized(task.lock) {
        if (task.state != TimerTask.VIRGIN)
            throw new IllegalStateException(
                "Task already scheduled or cancelled");
        task.nextExecutionTime = time;
        task.period = period;
        task.state = TimerTask.SCHEDULED;
    }

    // 最后,把新的 task 添加到关联队列里面
    queue.add(task);

    // 这里,会使用打 TimerQueue 对象的 getMin() 方法,这个方法是获取到接下来将要被执行的TimerTask 对象
    // 这里的逻辑是check 新添加的 task 对象是不是接下来马上会被执行
    // 如果刚添加的对象是需要马上执行的话,会使用  queue.notify 来通知在等待的线程。

    // 那么,会有谁在等待这个 notify 呢?是TimerThread 内部,TimerThread 会有一个死循环,在不停从queue中取任务来执行
    // 当queue为空的时候,TimerThread 会进行 queue.wait() 来进行休眠的状态,直到有新的来任务来唤醒它
    // 下面的代码就是,当queue为空的时候,这个判断条件会成立,然后就通知 TimerThread 重新唤醒
    // 当然,下面的条件成立也不全是 queue 为空的情况下
    if (queue.getMin() == task)
        queue.notify();
}

}

2.2 TimerTask 的源码分析

接下来,本文将会分析 TimerTask 的源码。相对于Timer 来说,它的源码其实很简单,TimerTask 是实现了Runnable 接口,同时也是一个抽象类,它并没有对 Runnable 的 run() 方法提供实现,而是需要子类来实现。

它对外提供了以下几个功能:

包含有一段可以执行的代码(实现的Runnable 接口的run方法)包含状态的定义。它有一个固定的状态:VIRGIN(新创建)、SCHEDULED(被调度进某一个 timer 的队列中了,但是还没有执行到)、EXECUTED(以及执行过了)、CANCELLED(任务被取消了)。包含有取消的方法。包含有获取下一次执行时间的方法。

相关的源码如下:

// 取消该任务 public boolean cancel() { synchronized(lock) { boolean result = (state == SCHEDULED); state = CANCELLED; return result; } }

// 根据执行周期,和设置的执行时间,来确定Task的下一次执行时间。 public long scheduledExecutionTime() { synchronized(lock) { // 其中,period 的值分为3种情况: // 取值为0: 表示该Task是非周期性任务; // 取值为正数: 表示该Task 是立即执行没有delay的周期性任务,period 的数值表示该Task 的周期 // 取值为负数: 表示该Task 是有 delay 的周期性任务,period 相反数是该Task 的周期 return (period < 0 ? nextExecutionTime + period : nextExecutionTime - period); } }

2.3 TimerThread 的源码分析

TimerThread 首先是一个 Thread 的子类,而且我们知道,在Java中,一个Thread 的对象就是代表了一个JVM虚拟机线程。那么,这个 TimerThread 其实也就是一个线程。

对于一个线程来说,那么它的关键就是它的 run() 方法,在调用线程的 start() 方法启动线程之后,接下来就会执行线程的 run() 方法,我们看下 TimerThread 的run() 方法:

public void run() { try { // 启动 mainLoop() 方法,这是一个阻塞方法,正常情况下会一只阻塞在这里 // 当 mainLoop() 执行完毕的时候,也即是这个线程退出的时候。 mainLoop(); } finally { // Someone killed this Thread, behave as if Timer cancelled // 做一些收尾工作 synchronized(queue) { newTasksMayBeScheduled = false; queue.clear(); // Eliminate obsolete references } } }

从以上可以明确得看出,TimerThread 里的实现是调用 mainLoop() 启动了一个死循环,这个死循环内部的工作就是这个线程的具体工作了,一旦线程的死循环执行完毕,线程的 run 方法就执行完了,线程紧接着就退出了。熟悉Android的朋友可能已经觉得这里的实现非常眼熟了,没错,这里的实现和Android平台的 Handler + HandlerThread + Looper 的机制非常相像,可以认为Android平台最初研发这套机制的时候,就是参考的Timer 的机制,然后在上面做了些升级和适合Android平台的一些改动。

下面是 mainLoop() 方法:

private void mainLoop() { // 一个死循环 while (true) { try { TimerTask task; boolean taskFired; synchronized(queue) { // 会等待到队列不为空,结合上面章节的分析,我们可以确定在新添加 TimerTask 到queue中的时候 // 会触发到 queue.notify() 然后通知到这里。 while (queue.isEmpty() && newTasksMayBeScheduled) queue.wait();

            // queue 为空,说明 timer 被取消了
            if (queue.isEmpty())
                break; // Queue is empty and will forever remain; die


            long currentTime, executionTime;
            // 又一次看到这个 queue.getMin() ,这个是根据接下来的执行时间来获取下一个需要被执行的任务
            task = queue.getMin();

            // 需要修改 task对象的内部数值,使用synchronized 保证线程安全
            synchronized(task.lock) {
                // TimerTask 有多种状态,一旦一个 TimerTask 被取消之后,它就不会被执行了。
                if (task.state == TimerTask.CANCELLED) {
                    queue.removeMin();
                    continue;  // No action required, poll queue again
                }

                // 获取到当前时间,和这个取出来的task 的下一次执行时间
                currentTime = System.currentTimeMillis();
                executionTime = task.nextExecutionTime;

                // 这里会check 当前这个 task 是不是已经到时间了
                // 这里会把是否到时间了这个状态保存在 taskFired 里面
                if (taskFired = (executionTime<=currentTime)) {
                    // 根据上文的分析,TimerTask 根据 task.period 值的不同,被分为3种类型
                    // 这里的 task.period == 0 的情况,是对应于一个非周期性任务
                    if (task.period == 0) {
                        // 非周期性任务,处理完就完事了,改状态,移除队列
                        queue.removeMin();
                        task.state = TimerTask.EXECUTED;
                    } else {
                        // 周期性任务,会被重新调度,也不会被移除队列
                        queue.rescheduleMin(
                          task.period<0 ? currentTime   - task.period
                                        : executionTime + task.period);
                    }
                }
            }

            // 这里是另一个会等待的地方,这个是为了等待任务的到期,等待时间就是距离到执行之间的时长
            if (!taskFired)
                queue.wait(executionTime - currentTime);
        }

        // taskFired 变量经过上面的步骤以及判断过了,如果是 true,说明task以及到时间了
        // 到时间就运行完事。
        if (taskFired)
            task.run();
    } catch(InterruptedException e) {
    }
}

}

总结

定时任务采用单线程的任务存放在数组中 利用数组实现优先级队列 根元素放在数组下标index =1 的位置。