当前位置:网站首页>延时队列`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 任务
作用
能够用来处理延时任务
如把衣服放到洗衣机过了段时间才过来处理等任务
边栏推荐
猜你喜欢
SQL injection bypass (I)
2022 le fichier CISP - Pte (i) contient:
On gpu: historical development and structure
2022 CISP-PTE(一)文件包含
【OpenAirInterface5g】RRC NR解析之RrcSetupComplete
[graduation season] graduation is the new beginning of your life journey. Are you ready
Interviewer: please introduce cache penetration, cache null value, cache avalanche and cache breakdown, which are easy to understand
POI replacing text and pictures in docx
内存屏障今生之Store Buffer, Invalid Queue
Interviewer: how to never migrate data and avoid hot issues by using sub database and sub table?
随机推荐
Visual studio vs shortcut key usage
Modeling competition - optical transport network modeling and value evaluation
在线文本数字识别列表求和工具
Redis cache penetration, cache breakdown, cache avalanche
2022 cisp-pte (I) document contains
poi导出excle
Thesis reading skills
快速实现蓝牙iBeacn功能详解
(已解决) npm突然报错 Cannot find module ‘D:\Program Files\nodejs\node_modules\npm\bin\npm-cli.js‘
专业四第二周自测
The fourth question of the 299th weekly match 6103 Minimum fraction of edges removed from the tree
webscoket 数据库监听
Meaning of 0.0.0.0:x
大学数据库mysql
Easyexcel: read Excel data into the list set
OPPO面试整理,真正的八股文,狂虐面试官
Assembly language - Wang Shuang Chapter 11 flag register - Notes
Mathematical modeling contest for graduate students - optimal application of UAV in rescue and disaster relief
Tidb database Quick Start Guide
SQL考勤查询间隔一小时