Timer

public class Timer1 {

    private final TaskQueue queue = new TaskQueue();//这是一个最小堆,它存放所有TimerTask。一个数组

    //定时任务只会创建一个线程,所以如果存在多个任务,且任务时间过长,超过了两个任务的间隔时间
    private final TimerThread thread = new TimerThread(queue);//queue中的任务,执行完从任务队列中移除。

    /**
     * This object causes the timer's task execution thread to exit
     * gracefully when there are no live references to the Timer object and no
     * tasks in the timer queue.  It is used in preference to a finalizer on
     * Timer as such a finalizer would be susceptible to a subclass's
     * finalizer forgetting to call it.

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

    private final static AtomicInteger nextSerialNumber = new AtomicInteger(0);
    private static int serialNumber() {
        return nextSerialNumber.getAndIncrement();
    }

    public Timer1() {
        this("Timer-" + serialNumber());
    }

    public Timer1(boolean isDaemon) {
        this("Timer-" + serialNumber(), isDaemon);//是否守护线程
    }

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

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

    public void schedule(TimerTask1 task, long delay) {//在时间等于或超过time的时候执行且只执行一次task,
        if (delay < 0)
            throw new IllegalArgumentException("Negative delay.");
        sched(task, System.currentTimeMillis()+delay, 0);
    }

    public void schedule(TimerTask1 task, Date time) {
        sched(task, time.getTime(), 0);
    }

    public void schedule(TimerTask1 task, long delay, long period) {//在时间等于或超过time的时候首次执行task,之后每隔period毫秒重复执行一次task 。
        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(TimerTask1 task, Date firstTime, long period) {
        if (period 0)
            throw new IllegalArgumentException("Non-positive period.");
        sched(task, firstTime.getTime(), -period);
    }

    public void scheduleAtFixedRate(TimerTask1 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(TimerTask1 task, Date firstTime,
                                    long period) {
        if (period 0)
            throw new IllegalArgumentException("Non-positive period.");
        sched(task, firstTime.getTime(), period);
    }

    private void sched(TimerTask1 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 != TimerTask1.VIRGIN)
                    throw new IllegalStateException(
                        "Task already scheduled or cancelled");
                task.nextExecutionTime = time;
                task.period = period;
                task.state = TimerTask1.SCHEDULED;
            }
            queue.add(task);
            //当timer对象调用schedule方法时,都会向队列添加元素,并唤醒TaskQueue队列上的线程,
            //这时候TimerThread会被唤醒,继续执行mainLoop方法。
            if (queue.getMin() == task)
                queue.notify();//多线程对同一队列出队入队,使用synchronized,queue.notify()
        }
    }

    public void cancel() {
        synchronized(queue) {
            thread.newTasksMayBeScheduled = false;
            queue.clear();
            queue.notify();  // In case queue was already empty.
        }
    }

     public int purge() {
         int result = 0;
         synchronized(queue) {
             for (int i = queue.size(); i > 0; i--) {
                 if (queue.get(i).state == TimerTask1.CANCELLED) {
                     queue.quickRemove(i);
                     result++;
                 }
             }
             if (result != 0)
                 queue.heapify();
         }
         return result;
     }
}

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;

    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
            }
        }
    }

    private void mainLoop() {//拿出任务队列中的第一个任务,如果执行时间还没有到,则继续等待,否则立即执行。
        while (true) {//函数执行的是一个死循环
            try {
                TimerTask1 task;
                boolean taskFired;
                synchronized(queue) {//并且加了queue锁,从而保证是线程安全的。
                    // 队列为空等待
                    while (queue.isEmpty() && newTasksMayBeScheduled)
                        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();
                    /*
                    queue.getMin()找到任务队列中执行时间最早的元素,
                      然后判断元素的state,period,nextExecutionTime,SCHEDULED等属性,从而确定任务是否可执行。
                       主要是判断这几个属性:1,state 属性,如果为取消(即我们调用了timer的cancel方法取消了某一任务),
                       则会从队列中删除这个元素,然后继续循环;2,period 属性,如果为单次执行,这个值为0,周期执行的话,
                       为我们传入的intervalTime值,如果为0,则会移出队列,并设置任务状态为已执行,然后下面的 task.run()会执行任务,
                       如果这个值不为0,则会修正队列,设置这个任务的再一次执行时间,queue.rescheduleMin这个函数来完成的这个操作; 3,taskFired
                       属性, 如果 executionTime*/
                    synchronized(task.lock) {
                        if (task.state == TimerTask1.CANCELLED) {
                            queue.removeMin();
                            continue;  // No action required, poll queue again
                        }
                        currentTime = System.currentTimeMillis();
                        executionTime = task.nextExecutionTime;
                        if (taskFired = (executionTimecurrentTime)) {
                            if (task.period == 0) { // Non-repeating, remove
                                queue.removeMin();
                                task.state = TimerTask1.EXECUTED;
                            } else { // Repeating task, reschedule
                                queue.rescheduleMin(
                                  task.period<0 ? currentTime   - task.period
                                                : executionTime + task.period);
                            }
                        }
                    }
                    //会对TaskQueue队列的首元素进行判断,看是否达到执行时间,
                    //如果没有,则进行休眠,休眠时间为队首任务的开始执行时间到当前时间的时间差。
                    if (!taskFired)
                        queue.wait(executionTime - currentTime);
                }
                if (taskFired)
                    task.run();
            } catch(InterruptedException e) {
            }
        }
    }
}

class TaskQueue {

    /*TaskQueue是一个平衡二叉堆,具有最小 nextExecutionTime 的 TimerTask 在队列中为 queue[1] ,
                  也就是堆中的根节点。第 n 个位置 queue[n] 的子节点分别在 queue[2n] 和 queue[2n+1]

                  也就是说TimerTask 在堆中的位置其实是通过nextExecutionTime 来决定的。
      nextExecutionTime 越小,那么在堆中的位置越靠近根,越有可能先被执行。而nextExecutionTime意思就是下一次执行开始的时间。
    */
    private TimerTask1[] queue = new TimerTask1[128];//默认128

    private int size = 0;

    int size() {
        return size;
    }

    void add(TimerTask1 task) {//根据执行时间的先后对数组元素进行排序,从而确定最先开始执行的任务,
        if (size + 1 == queue.length)
            queue = Arrays.copyOf(queue, 2*queue.length);
        queue[++size] = task;
        fixUp(size);
    }

    TimerTask1 getMin() {
        return queue[1];
    }

    TimerTask1 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 */
    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);
    }

    boolean isEmpty() {
        return size==0;
    }

    void clear() {
        // Null out task references to prevent memory leak
        for (int i=1; 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;
            TimerTask1 tmp = queue[j];  queue[j] = queue[k]; queue[k] = tmp;
            k = j;
        }
    }

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

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

Original: https://www.cnblogs.com/yaowen/p/13431627.html
Author: 哈哈呵h
Title: Timer

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/546755/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

  • ORA-01536: space quota exceeded for tablespace案例

    最近在做数据治理的过程中,回收了部分账号的权限,因为角色RESOURCE里拥有CREATE TABLE的权限,所以我想回收RESOURCE角色。例如,对于TEST账号,收回其创建表…

    技术杂谈 2023年5月30日
    089
  • Maskrom模式

    启动模式 ROC-RK3308-CC 有四种启动模式: Loader 模式 MaskRom 模式 Recovery 模式 Normal 模式 Loader 模式下,可以进行固件的烧…

    技术杂谈 2023年5月30日
    096
  • 分布式事务框架Seata入门案例

    Seata Server 部署 Seata分TC、TM和RM三个角色,TC(Server端)为单独服务端部署,TM和RM(Client端)由业务系统集成。 首先,下载最新的安装包 …

    技术杂谈 2023年7月23日
    081
  • exp

    想将远程机器上的数据库备份,搞了一上午,终于解决了: exp 用户名/密码@ip/service名 file=地址+文件名 Original: https://www.cnblog…

    技术杂谈 2023年5月31日
    094
  • 设计模式-适配器模式

    类型:结构型 目的:解决接口不兼容问题。 话不多说,看个案例吧。 优化案例 在真实的开发场景中,系统的每个模块都是分配给不同的团队或个人来开发的。这使得事前沟通变得尤为重要,且沟通…

    技术杂谈 2023年7月11日
    068
  • Swift开发iOS应用过程中的问题和解决记录

    虚拟机里安装OSX+XCode开发环境 用真机的请直接跳过这个部分。 主要是在VitrualBox里安装mac系统和xcode,参考这篇教程,VirtualBox的版本是4.3.1…

    技术杂谈 2023年5月31日
    0122
  • manim 4.0 预备知识

    1、Vmobject的认识 由handles和anchors决定,可以改变Vmobject.points来实现 2、动画的认识 最重要的是补间(interpolate),其由alp…

    技术杂谈 2023年7月24日
    094
  • 巴西针对电源产品的新法规

    巴西ANATEL近日发布了一项新的5159法案,该法案内容主要是关于移动电话使用的有线和无线电源和充电器。 法案称为《手机用充电器合格评定技术要求及测试程序》,涵盖了产品安全性和E…

    技术杂谈 2023年6月21日
    081
  • 【cartographer_ros】八:官方Demo参数配置和效果

    上一节介绍cartographer的主要配置参数。本节会研究一下这些参数改动,对算法的影响和效果,cartographer的调参一直是一个比较复杂的过程。 (1):调整本地 SLA…

    技术杂谈 2023年7月24日
    051
  • 分布式ID算法uuid,snowflake,leaf

    分布式ID算法uuid,snowflake,leaf SnowFlake 算法,是 Twitter 开源的分布式 id 生成算法。其核心思想就是:使用一个 64 bit 的 lon…

    技术杂谈 2023年5月31日
    087
  • 网盘搜索

    404. 抱歉,您访问的资源不存在。 可能是网址有误,或者对应的内容被删除,或者处于私有状态。 代码改变世界,联系邮箱 contact@cnblogs.com 园子的商业化努力-困…

    技术杂谈 2023年5月31日
    091
  • pip 如何下载 whl 环境到无外网机器

    你的测试机肯定是有外网,脚本肯定也能在测试机跑通。 先导出 whl 包列表到txt: 然后执行下载到当前目录: 将 whl 拷贝到内网服务器安装即可。(Win和Linux编译不互通…

    技术杂谈 2023年6月21日
    0101
  • Windows下 webpack4.0 的安装

    这里我们通过npm来进行安装 1. 安装 webpack // 全局安装webpack npm install webpack -g 2. 通过 webpack -v 命令查看当前…

    技术杂谈 2023年5月31日
    0109
  • 一款非常棒的十六进制编辑器 —— 010 Editor

    参考 https://zhuanlan.zhihu.com/p/96001673 插件 ELF.bt 用来分析ELF文件,用起来感觉像wireshark,可以高亮源文件中正常查看的…

    技术杂谈 2023年5月31日
    0104
  • 高仿MT4行情终端(K线图+操控+简单架构)

    本Demo讲述的范畴: K线的展示(小键盘方向操作,鼠标操作),QT的使用,客户端大致的框架展示。 开发环境: win10 64 位+VS2015 Update3 + QT 5.1…

    技术杂谈 2023年5月31日
    065
  • 王阳明心学精髓60句,带您寻找内心深处的光明(顶级人生智慧)

    1、天地载道,道存则万物生,道失则万物灭。 2、天道之数,至则反,盛则衰。炎炎之火,灭期近矣。 3、自知者智,自胜者勇,自暴者弃,自强者成。 4、夫用人之道,疑则生怨,信则共举。 …

    技术杂谈 2023年6月1日
    087
亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球