当前位置:网站首页>DelayQueue延迟队列的使用和场景
DelayQueue延迟队列的使用和场景
2022-07-05 07:11:00 【泡^泡】
一个实现PriorityBlockingQueue实现延迟获取的无界队列,在创建元素时,可以指定多久才能从队列中获取当前元素。只有延时期满后才能从队列中获取元素。
使用场景:
1.定时任务调度。使用DelayQueue保存当天将会执行的任务和执行时间,一旦从DelayQueue中获取到任务就开始执行,从比如TimerQueue就是使用DelayQueue实现的。
2.缓存系统的设计:可以用DelayQueue保存缓存元素的有效期,使用一个线程循环查询DelayQueue,一旦能从DelayQueue中获取元素时,表示缓存有效期到了。
3.订单延迟支付关闭。(MQ延迟消息队列/时间轮/定时任务)
代码案例
package thread;
import java.util.concurrent.Delayed;
import java.util.concurrent.TimeUnit;
public class DelayQueueExampleTask implements Delayed {
private String orderId;
private long start = System.currentTimeMillis();
private long time;
public DelayQueueExampleTask(String orderId,long time) {
this.orderId = orderId;
this.time = time;
}
@Override
public long getDelay(TimeUnit unit) {
return unit.convert((start+time)-System.currentTimeMillis(),TimeUnit.MILLISECONDS);
}
@Override
public int compareTo(Delayed o) {
return (int)(this.getDelay(TimeUnit.MILLISECONDS)-o.getDelay(TimeUnit.MILLISECONDS));
}
@Override
public String toString() {
return "DelayQueueExampleTask{" +
"orderId='" + orderId + '\'' +
", start=" + start +
", time=" + time +
'}';
}
}
package thread;
import java.util.concurrent.DelayQueue;
public class DelayQueueExampleTaskTest {
private static DelayQueue<DelayQueueExampleTask> delayQueue = new DelayQueue();
public static void main(String[] args) {
delayQueue.offer(new DelayQueueExampleTask("1001",1000));
delayQueue.offer(new DelayQueueExampleTask("1002",2000));
delayQueue.offer(new DelayQueueExampleTask("1003",3000));
delayQueue.offer(new DelayQueueExampleTask("1004",5000));
delayQueue.offer(new DelayQueueExampleTask("1005",6000));
delayQueue.offer(new DelayQueueExampleTask("1006",7000));
delayQueue.offer(new DelayQueueExampleTask("1007",8000));
delayQueue.offer(new DelayQueueExampleTask("1008",3000));
while(true){
try {
DelayQueueExampleTask task = delayQueue.take();
System.out.println(task);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
执行效果:
边栏推荐
- Concurrent programming - how to interrupt / stop a running thread?
- Build a microservice cluster environment locally and learn to deploy automatically
- Netease to B, soft outside, hard in
- Mid 2022 documentary -- the experience of an ordinary person
- MySQL setting trigger problem
- Ros2 - first acquaintance with ros2 (I)
- Special training of C language array
- cgroup_ memcg
- 【MySQL8.0不支持表名大写-对应方案】
- Binary search (half search)
猜你喜欢

网易To B,柔外刚中

Ros2 - workspace (V)
![[untitled]](/img/d5/2ac2b15818cf66c241e307c6723d50.jpg)
[untitled]

cgroup_ memcg

An article was opened to test the real situation of outsourcing companies

Sre core system understanding

SD_ CMD_ SEND_ SHIFT_ REGISTER

SD_ CMD_ RECEIVE_ SHIFT_ REGISTER
![[software testing] 06 -- basic process of software testing](/img/fe/3d8b9b68f95ac7899ab87d6993284d.jpg)
[software testing] 06 -- basic process of software testing

PostMessage communication
随机推荐
Spinningup drawing curve
逻辑结构与物理结构
SD_ CMD_ SEND_ SHIFT_ REGISTER
Inftnews | drink tea and send virtual stocks? Analysis of Naixue's tea "coin issuance"
ROS2——工作空间(五)
基于Cortex-M3、M4的GPIO口位带操作宏定义(可总线输入输出,可用于STM32、ADuCM4050等)
Xavier CPU & GPU 高负载功耗测试
In C language, int a= 'R'
【软件测试】05 -- 软件测试的原则
Interpretation of the earliest sketches - image translation work sketchygan
Instruction execution time
The difference between new and malloc
Sre core system understanding
window navicat连接阿里云服务器mysql步骤及常见问题
PHY驱动调试之 --- MDIO/MDC接口22号和45号条款(一)
postmessage通信
现在有html文件,和用vs制作的mvc(连接了数据库),怎么两个相连?
全局变量和静态变量的初始化
一文揭开,测试外包公司的真实情况
UIO driven framework