当前位置:网站首页>延时队列`DelayQueue`
延时队列`DelayQueue`
2022-06-27 06:50:00 【玲珑·】
代码
package com.koala.learn.charter1;
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;
import java.util.concurrent.Delayed;
import java.util.concurrent.TimeUnit;
/** * @Description TODO * @Date 2022/2/26 12:18 * @Created by koala */
@AllArgsConstructor
@NoArgsConstructor
class DelayElements implements Delayed {
private String key;
// 存活时间
private long aliveTime;
// 过期时间
private long timeOut;
public long getTimeOut() {
return timeOut;
}
public void setTimeOut(long timeOut) {
this.timeOut = timeOut;
}
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
public long getAliveTime() {
return aliveTime;
}
public void setAliveTime(long aliveTime) {
this.aliveTime = aliveTime;
}
public DelayElements(String key, long aliveTime) {
super();
this.key = key;
this.aliveTime = aliveTime;
this.timeOut = TimeUnit.NANOSECONDS.convert(aliveTime, TimeUnit.SECONDS) + System.nanoTime();
}
@Override
public int compareTo(Delayed o) {
if (o == null)
return 1;
if (o == this)
return 0;
if (o instanceof DelayElements) {
DelayElements de = (DelayElements) o;
if (this.timeOut > de.getTimeOut()) {
return 1;
} else if (this.timeOut < de.getTimeOut()) {
return -1;
}
}
return 0;
}
@Override
public long getDelay(TimeUnit unit) {
return unit.convert(timeOut - System.nanoTime(), TimeUnit.NANOSECONDS);
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((key == null) ? 0 : key.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
DelayElements other = (DelayElements) obj;
if (key == null) {
if (other.key != null)
return false;
} else if (!key.equals(other.key))
return false;
return true;
}
}
package com.koala.learn.charter1;
import org.junit.jupiter.api.Test;
import java.util.concurrent.DelayQueue;
/** * 学习延时队列 * 每隔十分钟就给思甜发一个消息 * @Date 2022/2/26 12:04 * @Created by koala */
public class Info {
public static void main(String[] args) throws InterruptedException {
DelayQueue<DelayElements> delayQueue = new DelayQueue();
// 创建消息内容
String msg = "你好 sitian";
delayQueue.offer(new DelayElements(msg,1));
delayQueue.offer(new DelayElements(msg,3));
delayQueue.offer(new DelayElements(msg,5));
while(!delayQueue.isEmpty()){
DelayElements poll = delayQueue.take();
System.out.println(poll.getKey());
}
}
}
思想
建立一个任务 设置定时时间 放入队列
取任务每次取最近的任务
触发
工作上遇到建立了checklist 任务 博客在完成了checklist 任务
作用
能够用来处理延时任务
如把衣服放到洗衣机过了段时间才过来处理等任务
边栏推荐
- Compatibility comparison between tidb and MySQL
- extendible hashing
- Centos7.9 install MySQL 5.7 and set startup
- 小米面试官:听你说精通注册中心,我们来聊 3 天 3 夜
- 技术人员创业一年心得
- yarn create vite 报错 ‘D:\Program‘ 不是内部或外部命令,也不是可运行的程序 或批处理文件
- Interviewer: you use Lombok every day. What is its principle? I can't answer
- Assembly language - Wang Shuang Chapter 9 Principles of transfer instructions - Notes
- 高薪程序员&面试题精讲系列116之Redis缓存如何实现?怎么发现热key?缓存时可能存在哪些问题?
- Assembly language - Wang Shuang Chapter 11 flag register - Notes
猜你喜欢

Fractional Order PID control

mssql如何使用语句导出并删除多表数据

Classical cryptosystem -- substitution and replacement

Vs how to configure opencv? 2022vs configuration opencv details (multiple pictures)

Assembly language - Wang Shuang Chapter 9 Principles of transfer instructions - Notes

Modeling competition - optical transport network modeling and value evaluation

面试官:用分库分表如何做到永不迁移数据和避免热点问题?

Matlab GUI interface simulation DC motor and AC motor speed simulation

AHB2APB桥接器设计(2)——同步桥设计的介绍

Interviewer: you use Lombok every day. What is its principle? I can't answer
随机推荐
On gpu: historical development and structure
One year's experience of technical personnel in Entrepreneurship
Compatibility comparison between tidb and MySQL
【OpenAirInterface5g】RRC NR解析之RrcSetupComplete
内存屏障今生之Store Buffer, Invalid Queue
如何优雅的写 Controller 层代码?
winow10安装Nexus nexus-3.20.1-01
将通讯录功能设置为数据库维护,增加用户名和密码
程序人生 - 程序员三十五岁瓶颈你怎么看?
获取地址url中的query参数指定参数方法
yarn create vite 报错 ‘D:\Program‘ 不是内部或外部命令,也不是可运行的程序 或批处理文件
extendible hashing
2022 CISP-PTE(一)文件包含
The number of query results of maxcompute SQL is limited to 1W
从5秒优化到1秒,系统飞起来了...
Unrecognized VM option ‘‘
Centos7.9安装mysql 5.7,并设置开机启动
SQL考勤查询间隔一小时
面试官:用分库分表如何做到永不迁移数据和避免热点问题?
快速实现单片机和手机蓝牙通信