多线程——定时器

定时器在日常开发中常用到的组件工具,类似于“闹钟”
设定一个时间,到了时间定时器就会自动去执行某个逻辑

Java标准库,也提供了定时器的实现

Timer timer = new Timer( );

代码演示:

public class Test14 {
    public static void main(String[] args) {
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                System.out.println("三秒");
            }
        },3000);
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                System.out.println("两秒");
            }
        },2000);
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                System.out.println("一秒");
            }
        },1000);
        
     System.out.println("main");
    }
}

运行结果:

main
一秒
两秒
三秒

由此可见,定时器不是按照代码的先后顺序执行的,而是按照给定时间的先后顺序执行的

如何手动实现定时器?

手动实现的MyTimer里面要包含哪些内容
1.需要一个线程,负责掐时间,等到任务到了执行时间,该线程就会负责执行
2.还需要一个队列/数组,能够保存所有的schedule进来的任务
每个任务都带有等待时间delay,所以使用优先级队列,等待时间delay短的先执行

首先要创建一个类,用来描述一个任务
该类中要有执行时间和要执行的代码

class MyTimerTask implements Comparable<MyTimerTask> {
    // 在什么时间点来执行这个任务.
    // 此处约定这个 time 是一个 ms 级别的时间戳.
    private long time;
    // 实际任务要执行的代码.
    private Runnable runnable;

    public long getTime() {
        return time;
    }

    // delay 期望是一个 "相对时间"
    public MyTimerTask(Runnable runnable, long delay) {
        this.runnable = runnable;
        // 计算一下真正要执行任务的绝对时间. (使用绝对时间, 方便判定任务是否到达时间的)
        this.time = System.currentTimeMillis() + delay;
    }

    public void run() {
        runnable.run();
    }

    @Override
    public int compareTo(MyTimerTask o) {
        return (int) (this.time - o.time);
        // return (int) (o.time - this.time);
    }
}

然后把每个任务都放进优先级队列里面进行比较,任务等待时间短的先执行

class MyTimer {
    // 负责扫描任务队列, 执行任务的线程.
    private Thread t = null;
    // 任务队列
    private PriorityQueue<MyTimerTask> queue = new PriorityQueue<>();
   
  
    public void schedule(Runnable runnable, long delay) {
      
            MyTimerTask task = new MyTimerTask(runnable, delay);
            queue.offer(task);
            
          
        }
    }

取出队头,使用线程去执行

public MyTimer() {
        t = new Thread(() -> {
            // 扫描线程就需要循环的反复的扫描队首元素, 然后判定队首元素是不是时间到了.
            // 如果时间没到, 啥都不干
            // 如果时间到了, 就执行这个任务并且把这个任务从队列中删除掉.
            while (true) {
                try {
                   
                        while (queue.isEmpty()) {
                            // 暂时先不处理
                           
                        }
                        MyTimerTask task = queue.peek();
                        // 获取到当前时间
                        long curTime = System.currentTimeMillis();
                        if (curTime >= task.getTime()) {
                            // 当前时间已经达到了任务时间, 就可以执行任务了.
                            queue.poll();
                            task.run();
                        } else {
                            // 当前时间还没到, 暂时先不执行
                            // 不能使用 sleep. 会错过新的任务, 也无法释放锁.
                            // Thread.sleep(task.getTime() - curTime);
                            
                        }
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        // 要记得 start !!!!
        t.start();
    }

代码还存在线程安全问题,对代码进行优化:
完整代码:

import java.util.PriorityQueue;

// 通过这个类, 来描述一个任务
class MyTimerTask implements Comparable<MyTimerTask> {
    // 在什么时间点来执行这个任务.
    // 此处约定这个 time 是一个 ms 级别的时间戳.
    private long time;
    // 实际任务要执行的代码.
    private Runnable runnable;

    public long getTime() {
        return time;
    }

    // delay 期望是一个 "相对时间"
    public MyTimerTask(Runnable runnable, long delay) {
        this.runnable = runnable;
        // 计算一下真正要执行任务的绝对时间. (使用绝对时间, 方便判定任务是否到达时间的)
        this.time = System.currentTimeMillis() + delay;
    }

    public void run() {
        runnable.run();
    }

    @Override
    public int compareTo(MyTimerTask o) {
        return (int) (this.time - o.time);
        // return (int) (o.time - this.time);
    }
}

// 通过这个类, 来表示一个定时器
class MyTimer {
    // 负责扫描任务队列, 执行任务的线程.
    private Thread t = null;
    // 任务队列
    private PriorityQueue<MyTimerTask> queue = new PriorityQueue<>();
    // 搞个锁对象, 此处使用 this 也可以.
    private Object locker = new Object();

    public void schedule(Runnable runnable, long delay) {
        synchronized (locker) {
            MyTimerTask task = new MyTimerTask(runnable, delay);
            queue.offer(task);
            // 添加新的元素之后, 就可以唤醒扫描线程的 wait 了.
            locker.notify();
        }
    }

    public void cancel() {
        // 结束 t 线程即可
        // interrupt
    }

    // 构造方法. 创建扫描线程, 让扫描线程来完成判定和执行.
    public MyTimer() {
        t = new Thread(() -> {
            // 扫描线程就需要循环的反复的扫描队首元素, 然后判定队首元素是不是时间到了.
            // 如果时间没到, 啥都不干
            // 如果时间到了, 就执行这个任务并且把这个任务从队列中删除掉.
            while (true) {
                try {
                    synchronized (locker) {
                        while (queue.isEmpty()) {
                            // 暂时先不处理
                            locker.wait();
                        }
                        MyTimerTask task = queue.peek();
                        // 获取到当前时间
                        long curTime = System.currentTimeMillis();
                        if (curTime >= task.getTime()) {
                            // 当前时间已经达到了任务时间, 就可以执行任务了.
                            queue.poll();
                            task.run();
                        } else {
                            // 当前时间还没到, 暂时先不执行
                            // 不能使用 sleep. 会错过新的任务, 也无法释放锁.
                            // Thread.sleep(task.getTime() - curTime);
                            locker.wait(task.getTime() - curTime);
                        }
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        // 要记得 start !!!!
        t.start();
    }
}

public class ThreadDemo31 {
    public static void main(String[] args) {
        MyTimer timer = new MyTimer();
        timer.schedule(new Runnable() {
            @Override
            public void run() {
                System.out.println("hello 3000");
            }
        }, 3000);
        timer.schedule(new Runnable() {
            @Override
            public void run() {
                System.out.println("hello 2000");
            }
        }, 2000);
        timer.schedule(new Runnable() {
            @Override
            public void run() {
                System.out.println("hello 1000");
            }
        }, 1000);
    }
}

运行结果:

hello 1000
hello 2000
hello 3000

  • 7
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Mang go

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值