当前位置:网站首页>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
- [OBS] x264 Code: "buffer_size“
- Import CV2, prompt importerror: libcblas so. 3: cannot open shared object file: No such file or directory
- 2022年中纪实 -- 一个普通人的经历
- Negative number storage and type conversion in programs
- inux摄像头(mipi接口)简要说明
- Now there are HTML files and MVC made with vs (connected to the database). How can they be connected?
- GPIO port bit based on Cortex-M3 and M4 with operation macro definition (can be used for bus input and output, STM32, aducm4050, etc.)
- Ros2 - ros2 vs. ros1 (II)
- 第 2 章:小试牛刀,实现一个简单的Bean容器
猜你喜欢
Powermanagerservice (I) - initialization
Import CV2 prompt importerror: libgl so. 1: Cannot open shared object file: no such file or directory
Use the Paping tool to detect TCP port connectivity
Steps and FAQs of connecting windows Navicat to Alibaba cloud server MySQL
【Node】nvm 版本管理工具
Ros2 - workspace (V)
C语言数组专题训练
Spinningup drawing curve
【软件测试】02 -- 软件缺陷管理
【软件测试】03 -- 软件测试概述
随机推荐
Energy conservation and creating energy gap
[untitled]
1290_ Implementation analysis of prvtaskistasksuspended() interface in FreeRTOS
2022年中纪实 -- 一个普通人的经历
[software testing] 02 -- software defect management
目标检测系列——Faster R-CNN原理详解
M2DGR 多源多场景 地面机器人SLAM数据集
NPM and package common commands
Ethtool principle introduction and troubleshooting ideas for network card packet loss (with ethtool source code download)
[vscode] prohibit the pylance plug-in from automatically adding import
纯碱是做什么的?
【软件测试】05 -- 软件测试的原则
ROS2——安装ROS2(三)
逻辑结构与物理结构
An article was opened to test the real situation of outsourcing companies
Brief description of inux camera (Mipi interface)
Do you choose pandas or SQL for the top 1 of data analysis in your mind?
并发编程 — 死锁排查及处理
IPage能正常显示数据,但是total一直等于0
Anaconda navigator click open no response, can not start error prompt attributeerror: 'STR' object has no attribute 'get‘