• 【多线程】定时器 Timer


    一. 定时器是什么

    定时器也是软件开发中的一个重要组件. 类似于一个 “闹钟”. 达到一个设定的时间之后, 就执行某个指定好的代码(任务).

    二. 标准库中的定时器

    • 标准库中提供了一个 Timer 类. Timer 类的核心方法为 schedule .
    • schedule 包含两个参数. 第一个参数指定即将要执行的任务代码, 第二个参数指定多长时间之后执行 (单位为毫秒).
            Timer timer = new Timer();
            timer.schedule(new TimerTask() {
                @Override
                public void run() {
                    System.out.println("hello");
                }
            }, 3000);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    三. 实现定时器

    定时器的构成:

    • 任务:创建一个专门的类 Task 表示一个定时器任务,Task 中带有一个时间属性。
    • 一个带优先级的阻塞队列来组织任务。
    • 同时有一个 worker 线程一直扫描队首元素, 看队首元素是否需要执行。

    为啥要带优先级呢?
    因为阻塞队列中的任务都有各自的执行时刻 (delay). 最先执行的任务一定是 delay 最小的. 使用带优先级的队列就可以高效的把这个 delay 最小的任务找出来,因为要使用优先级队列,所以说放入队列的任务要能够比较。

    实现定时器:

    1. Timer 类提供的核心接口为 schedule, 用于注册一个任务, 并指定这个任务多长时间后执行.
    public class Timer {
        public void schedule(Runnable command, long after) {
            // TODO
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    1. Task 类用于描述一个任务(作为 Timer 的内部类). 里面包含一个 Runnable 对象和一个 time(毫秒时间戳)
      这个对象需要放到 优先队列 中. 因此需要实现 Comparable 接口.
    static class Task implements Comparable<Task> {
        private Runnable command;
        private long time;
        public Task(Runnable command, long time) {
            this.command = command;
            // time 中存的是绝对时间, 超过这个时间的任务就应该被执行
            this.time = System.currentTimeMillis() + time;
        }
        public void run() {
            command.run();
        }
        @Override
        public int compareTo(Task o) {
            // 谁的时间小谁排前面
            return (int)(time - o.time);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    1. Timer 实例中, 通过 PriorityBlockingQueue 来组织若干个 Task 对象.
      通过 schedule 来往队列中插入一个个 Task 对象.
    class Timer {
        // 核心结构
        private PriorityBlockingQueue<Task> queue = new PriorityBlockingQueue();
    
        public void schedule(Runnable command, long after) {
            Task task = new Task(command, after);
            queue.offer(task);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    1. Timer 类中存在一个 worker 线程, 一直不停的扫描队首元素, 看看是否能执行这个任务. 所谓 “能执行” 指的是该任务设定的时间已经到达了.
    class Timer {
        // ... 前面的代码不变
    
        public Timer() {
            // 启动 worker 线程
            Worker worker = new Worker();
            worker.start();
        }
    
        class Worker extends Thread{
            @Override
            public void run() {
                while (true) {
                    try {
                        Task task = queue.take();
                        long curTime = System.currentTimeMillis();
                        if (task.time > curTime) {
                            // 时间还没到, 就把任务再塞回去
                            queue.put(task);
                        } else {
                            // 时间到了, 可以执行任务
                            task.run();
                        }
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                        break;
                    }
                }
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31

    但是当前这个代码中存在一个严重的问题, 就是 while (true) 转的太快了, 造成了无意义的 CPU 浪费.

    比如第一个任务设定的是 1 min 之后执行某个逻辑. 但是这里的 while (true) 会导致每秒钟访问队
    首元素几万次. 而当前距离任务执行的时间还有很久呢.
    
    • 1
    • 2
    1. 引入一个 mailBox 对象, 借助该对象的 wait / notify 来解决 while (true) 的忙等问题.
    class Timer {
        // 存在的意义是避免 worker 线程出现忙等的情况
        private Object mailBox = new Object();
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5

    修改 worker 的 run 方法, 引入 wait, 等待一定的时间.

    为什么不使用 sleep, 因为 sleep 不能被中途唤醒,新的任务可能在之前所有任务的最前面,此时 wait 可以由 notify 唤醒,而 sleep 不能。

        public void run() {
            while (true) {
                try {
                    Task task = queue.take();
                    long curTime = System.currentTimeMillis();
                    if (task.time > curTime) {
                        // 时间还没到, 就把任务再塞回去
                        queue.put(task);
                        // [引入 wait] 等待时间按照队首元素的时间来设定. 
                        synchronized (mailBox) {
                            // 指定等待时间 wait
                            mailBox.wait(task.time - curTime);
                        }
    
                    } else {
                        // 时间到了, 可以执行任务
                        task.run();
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                    break;
                }
            }
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    修改 Timer 的 schedule 方法, 每次有新任务到来的时候唤醒一下 worker 线程. (因为新插入的任务可能是需要马上执行的).

        public void schedule(Runnable command, long after) {
            Task task = new Task(command, after);
            queue.offer(task);
    
            // [引入 notify] 每次有新的任务来了, 都唤醒一下 worker 线程, 检测下当前是否有任务该执行
            synchronized (mailBox) {
                mailBox.notify();
            }
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    扫描线程里面的加锁位置还有问题,还可能发生线程安全问题:
    当扫描线程已经获取堆顶任务,判断出任务还不该执行,计算出等待时间,正准备 wait 时,该线程被调出 CPU,其他线程运行并插入了一个执行时间更近的任务,并进行了 notify, (但是打空了,扫描线程没有在 wait )当扫描线程重新获取 CPU 时,继续往下执行之前未执行的 wait,并且wait 的时间按照之前的任务算出来的,所以等待的时间太长了,导致错过了最新插入的那个任务的执行时间。

    举个栗子:

    • 当前时间是 8:00,扫描线程发现阻塞队列堆顶任务是 9:00 执行,所以打算 wait (1 小时)
    • 但是此时突然被调度出 CPU, 另外一个线程执行并往阻塞队列中 添加了 8: 30 执行的任务, 添加完成之后 notify, 但是 打空了, 没有 notify 到, 因为扫描线程并没有 wait, 而是因为其他原因被调出 CPU了
    • 等到扫描线程重新获得 CPU 后, 继续往下执行之前没执行的 wait 操作, wait 了 1 个 小时, 错过了 8:30 这个任务的执行时间

    解决:
    上面的主要原因就是 新加入任务了, 但是扫描线程没有感知到, 怎么办:
    要确定每次 notify 时都在 wait, 能够通知到, 怎么确定 :
    增大锁的粒度, 扫描线程获取元素的时候就加上锁, 这样 你想要 notify 就必须等 扫描线程 释放锁, 扫描线程什么时候释放锁 ? wait 的时候或者出代码块(出代码块后再进入代码块会重新获取堆顶元素)的时候, 所以 , 这就确定每次 新加入任务时, 扫描线程都能感知到.

        class Worker extends Thread{
            @Override
            public void run() {
                while (true) {
                    try {
                        synchronized (mailBox) {
                            Task task = queue.take();
                            // 不使用 peek 是因为当队列为空时,peek 不会阻塞,导致后面空指针异常。
                            // 并且因为阻塞队列由堆实现,再把元素放回堆,调整堆的时间复杂度也不高。
                            long curTime = System.currentTimeMillis();
                            if (task.time > curTime) {
                                // 时间还没到, 就把任务再塞回去
                                queue.put(task);
                                // 指定等待时间 wait
                                mailBox.wait(task.time - curTime);
                            } else {
                                // 时间到了, 可以执行任务
                                task.run();
                            }
                        }
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                        break;
                    }
                }
            }
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27

    完整代码:

    /**
     * 定时器的构成:
     * 一个带优先级的阻塞队列
     * 队列中的每个元素是一个 Task 对象.
     * Task 中带有一个时间属性, 队首元素就是即将执行的任务
     * 同时有一个 worker 线程一直扫描队首元素, 看队首元素是否需要执行
     */
    class Timer {
        static class Task implements Comparable<Task> {
            private Runnable command;
            private long time;
            public Task(Runnable command, long time) {
                this.command = command;
                // time 中存的是绝对时间, 超过这个时间的任务就应该被执行
                this.time = System.currentTimeMillis() + time;
            }
            public void run() {
                command.run();
            }
            @Override
            public int compareTo(Task o) {
                // 谁的时间小谁排前面
                return (int)(time - o.time);
            }
        }
        // 核心结构
        private PriorityBlockingQueue<Task> queue = new PriorityBlockingQueue();
        // 存在的意义是避免 worker 线程出现忙等的情况
        private Object mailBox = new Object();
    
        class Worker extends Thread{
            @Override
            public void run() {
                while (true) {
                    try {
                        synchronized (mailBox) {
                            Task task = queue.take();
                            // 不使用 peek 是因为当队列为空时,peek 不会阻塞,导致后面空指针异常。
                            // 并且因为阻塞队列由堆实现,再把元素放回堆,调整堆的时间复杂度也不高。
                            long curTime = System.currentTimeMillis();
                            if (task.time > curTime) {
                                // 时间还没到, 就把任务再塞回去
                                queue.put(task);
                                // 指定等待时间 wait
                                mailBox.wait(task.time - curTime);
                            } else {
                                // 时间到了, 可以执行任务
                                task.run();
                            }
                        }
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                        break;
                    }
                }
            }
        }
        public Timer() {
            // 创建 worker 线程
            Worker worker = new Worker();
            // 注意刚 创建 Timer 对象,就要启动扫描线程
            worker.start();
        }
    
        // 加入任务
        public void schedule(Runnable command, long after) {
            Task task = new Task(command, after);
            queue.offer(task);
            // 每次有新任务到来的时候唤醒一下 worker 线程. (因为新插入的任务可能是需要马上执行的).
            synchronized (mailBox) {
                mailBox.notify();
            }
        }
    
        public static void main(String[] args) {
            Timer timer = new Timer();
            Runnable command = new Runnable() {
                @Override
                public void run() {
                    System.out.println("我来了");
                    timer.schedule(this, 3000);
                }
            };
            timer.schedule(command, 3000);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86

    总结:

    1. 描述一个任务: Runnable + time 。
    2. 使用优先级阻塞队列组织若干任务,PriorityBlockingQueue。
    3. 使用 schedule 方法注册任务到队列中。
    4. 专门使用一个扫描线程,不停的获取队首元素并判断时间是否到达。

    注意:
    定时器中的任务不一定是准时执行的,因为只有一个线程在执行任务,所以说可能正在执行某个任务时,其他线程就该执行了,由于只有一个线程,其他的任务执行时间不得不推迟。所以说是一个线程按照任务执行时间的先后顺序执行任务,所以任务的真正执行时间很有可能延后。

  • 相关阅读:
    【PLC GX Works2】创建一个工程
    释放搜索潜力:基于ES(ElasticSearch)打造高效的语义搜索系统,让信息尽在掌握[2.项目讲解篇],支持Linux/Windows部署安装
    【Spring Boot】过滤敏感词的两种实现
    苹果CMS插件-苹果CMS全套插件免费
    应用统计-点估计法(1.矩估计 2.极大似然估计)
    讯优随身Wi-Fi(410)折腾
    golang中的unsafe.Pointer,指针,引用
    找出多组分辨率中最接近目标值的一组
    vue cli3.0 怎么配置 后端请求接口域名,区别打包环境及 vue项目域名的问题
    【Javascript】运算符(赋值,算术,自增,自减)
  • 原文地址:https://blog.csdn.net/m0_61832361/article/details/132812036