当前位置:网站首页>延时队列`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 任务
作用
能够用来处理延时任务
如把衣服放到洗衣机过了段时间才过来处理等任务
边栏推荐
- 内存屏障今生之Store Buffer, Invalid Queue
- One year's experience of technical personnel in Entrepreneurship
- 2022 cisp-pte (II) SQL injection
- The fourth question of the 299th weekly match 6103 Minimum fraction of edges removed from the tree
- 2022 CISP-PTE(一)文件包含
- HTAP Quick Start Guide
- (已解决) npm突然报错 Cannot find module ‘D:\Program Files\nodejs\node_modules\npm\bin\npm-cli.js‘
- mssql如何使用语句导出并删除多表数据
- POI export excle
- Tidb database Quick Start Guide
猜你喜欢

yarn create vite 报错 ‘D:\Program‘ 不是内部或外部命令,也不是可运行的程序 或批处理文件

Yolov6's fast and accurate target detection framework is open source

面试官:大量请求 Redis 不存在的数据,从而打倒数据库,你有什么方案?

【OpenAirInterface5g】RRC NR解析之RrcSetupComplete

Interviewer: you use Lombok every day. What is its principle? I can't answer

内存屏障今生之Store Buffer, Invalid Queue

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

Machine learning

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

2022 CISP-PTE(一)文件包含
随机推荐
一個人管理1000臺服務器?這款自動化運維工具一定要掌握
Matlab GUI interface simulation DC motor and AC motor speed simulation
Interviewer: how to never migrate data and avoid hot issues by using sub database and sub table?
YOLOv6又快又准的目标检测框架 已开源
Partial function of Scala
仙人掌之歌——投石问路(1)
Modeling competition - optical transport network modeling and value evaluation
poi导出excle
From 5 seconds to 1 second, the system flies
An Empirical Evaluation of In-Memory Multi-Version Concurrency Control
2022 CISP-PTE(一)文件包含
Scala之偏函数Partial Function
Assembly language - Wang Shuang Chapter 11 flag register - Notes
面试官:你天天用 Lombok,说说它什么原理?我竟然答不上来…
thrift
NoViableAltException([email protected][2389:1: columnNameTypeOrConstraint : ( ( tableConstraint ) | ( columnNameT
2022 cisp-pte (II) SQL injection
The number of query results of maxcompute SQL is limited to 1W
mysql关于自增和不能为空
sql sever列名或所提供值的数目与表定义不匹配