📎前言
✨本篇文章主要介绍多线程相关案例中定时器相关的问题,后续将持续更新自己的学习记录,以此来督促我不断地学习与进步.希望大家关注支持!✨
🚀🚀系列专栏:【多线程】
📻📻本章内容:定时器
1. 什么是定时器
定时器也是软件开发中的一个重要组件. 类似于一个 “闹钟”. 达到一个设定的时间之后, 就执行某个指定好的代码.
定时器是一种实际开发中非常常用的组件.
比如网络通信中, 如果对方 500ms 内没有返回数据, 则断开连接尝试重连.
比如一个 Map, 希望里面的某个 key 在 3s 之后过期(自动删除).
类似于这样的场景就需要用到定时器
2. 标准库中的定时器
- 标准库中提供了一个 Timer 类. Timer 类的核心方法为 schedule .
schedule
包含两个参数. 第一个参数指定即将要执行的任务代码, 第二个参数指定多长时间之后执行 (单位为毫秒)
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
System.out.println("hello");
}
}, 3000);
3. 定时器的实现
3.1 定时器的构成
- 一个带优先级的阻塞队列
为啥要带优先级呢?
因为阻塞队列中的任务都有各自的执行时刻 (delay). 最先执行的任务一定是 delay 最小的. 使用带优先级的队列就可以高效的把这个 delay 最小的任务找出来.
- 队列中的每个元素是一个 Task 对象.
- Task 中带有一个时间属性, 队首元素就是即将
- 同时有一个 worker 线程一直扫描队首元素, 看队首元素是否需要执行
3.2 实现过程
- Timer 类提供的核心接口为
schedule
, 用于注册一个任务, 并指定这个任务多长时间后执行.
public class Timer {
public void schedule(Runnable command, long after) {
// TODO
}
}
- 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);
}
}
}
- 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);
}
}
- 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;
}
}
}
}
}
但是当前这个代码中存在一个严重的问题, 就是 while (true) 转的太快了, 造成了无意义的 CPU 浪费.
- 引入一个 mailBox 对象, 借助该对象的
wait
/notify
来解决 while (true) 的忙等问题.
class Timer {
// 存在的意义是避免 worker 线程出现忙等的情况
private Object mailBox = new Object();
}
修改 Worker 的 run 方法, 引入 wait, 等待一定的时间.
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;
}
}
}
修改 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();
}
}
完整代码示例
import java.util.concurrent.PriorityBlockingQueue;
public 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 {
Task task = queue.take();
long curTime = System.currentTimeMillis();
if (task.time > curTime) {
// 时间还没到, 就把任务再塞回去
queue.put(task);
synchronized (mailBox) {
// 指定等待时间 wait
mailBox.wait(task.time - curTime);
}
} else {
// 时间到了, 可以执行任务
task.run();
}
} catch (InterruptedException e) {
e.printStackTrace();
break;
}
}
}
}
public Timer() {
// 启动 worker 线程
Worker worker = new Worker();
worker.start();
}
// schedule 原意为 "安排"
public void schedule(Runnable command, long after) {
Task task = new Task(command, after);
queue.offer(task);
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);
}
}
🎉总结
🎇到这里本篇文章就结束了,感谢大家的阅读。非常希望大家可以提出宝贵的建议!!
这篇文章主要介绍了多线程的相关案例中另外一个比较经典的案例 – 定时器. 后续还会介绍到其他的相关案例.
🎇本篇文章是博主对自己学习中所学知识的个人理解。日后我也将不断更新自己学习过程以达到温故知新。更多相关知识请关注博主其他文章.
🎇希望大家可以点赞+收藏+评论支持一下噢!
🎇继续保持关注~