当前位置:网站首页>Delayqueue usage and scenarios of delay queue
Delayqueue usage and scenarios of delay queue
2022-07-05 07:13:00 【Bubble ^ bubble】
An implementation PriorityBlockingQueue Realize the boundless queue of delay acquisition , When you create an element , You can specify how long it takes to get the current element from the queue . Only after the delay expires can the element be retrieved from the queue .
Use scenarios :
1. Scheduled task scheduling . Use DelayQueue Save the tasks and execution time that will be performed on that day , Once from DelayQueue Get the task and start executing , From, for example TimerQueue Is the use of DelayQueue Realized .
2. The design of cache system : It can be used DelayQueue Save the validity of the cache elements , Use a thread loop to query DelayQueue, Once you can get from DelayQueue When getting elements in , Indicates that the cache expiration date is .
3. Order late payment close .(MQ Delayed message queuing / Time wheel / Timing task )
Code case
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();
}
}
}
}
Execution effect :
边栏推荐
- Database SQL practice 3. Find the current salary details of the current leaders of each department and their corresponding department number Dept_ no
- 睿智的目标检测59——Pytorch Focal loss详解与在YoloV4当中的实现
- [tf] Unknown: Failed to get convolution algorithm. This is probably because cuDNN failed to initial
- Logical structure and physical structure
- The differences and connections among cookies, sessions, JWT, and tokens
- . Net core stepping on the pit practice
- Orin installs CUDA environment
- docker安装mysql并使用navicat连接
- mysql设置触发器问题
- Ros2 - function package (VI)
猜你喜欢
第 2 章:小试牛刀,实现一个简单的Bean容器
三体目标管理笔记
Build a microservice cluster environment locally and learn to deploy automatically
Use the Paping tool to detect TCP port connectivity
Ros2 - configuration development environment (V)
Negative number storage and type conversion in programs
程序中的负数存储及类型转换
PHY驱动调试之 --- PHY控制器驱动(二)
【软件测试】03 -- 软件测试概述
并发编程 — 如何中断/停止一个运行中的线程?
随机推荐
【软件测试】02 -- 软件缺陷管理
三体目标管理笔记
Database SQL practice 4. Find the last of employees in all assigned departments_ Name and first_ name
Netease to B, soft outside, hard in
Three body goal management notes
你心目中的数据分析 Top 1 选 Pandas 还是选 SQL?
*P++, (*p) + +, * (p++) differences
Mid 2022 documentary -- the experience of an ordinary person
SOC_SD_CMD_FSM
SD_ CMD_ SEND_ SHIFT_ REGISTER
Do you choose pandas or SQL for the top 1 of data analysis in your mind?
SOC_ SD_ CMD_ FSM
氢氧化钠是什么?
[tf1] save and load parameters
PHY drive commissioning --- mdio/mdc interface Clause 22 and 45 (I)
IPage能正常显示数据,但是total一直等于0
全局变量和静态变量的初始化
Implementation of one-dimensional convolutional neural network CNN based on FPGA (VIII) implementation of activation layer
二分查找(折半查找)
ROS2——工作空间(五)