当前位置:网站首页>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 :
边栏推荐
- 【软件测试】06 -- 软件测试的基本流程
- Volcano resource reservation feature
- Ros2 - ros2 vs. ros1 (II)
- Steps and FAQs of connecting windows Navicat to Alibaba cloud server MySQL
- Ros2 - node (VII)
- Executealways of unity is replacing executeineditmode
- SOC_ SD_ DATA_ FSM
- Import CV2, prompt importerror: libcblas so. 3: cannot open shared object file: No such file or directory
- Database SQL practice 3. Find the current salary details of the current leaders of each department and their corresponding department number Dept_ no
- Use the Paping tool to detect TCP port connectivity
猜你喜欢

Ros2 - ros2 vs. ros1 (II)

Mathematical analysis_ Notes_ Chapter 8: multiple integral

PHY drive commissioning --- mdio/mdc interface Clause 22 and 45 (I)

Concurrent programming - deadlock troubleshooting and handling

PHY驱动调试之 --- PHY控制器驱动(二)

Skywalking all

PostMessage communication

U-Boot初始化及工作流程分析

【软件测试】03 -- 软件测试概述

【软件测试】04 -- 软件测试与软件开发
随机推荐
Intelligent target detection 59 -- detailed explanation of pytoch focal loss and its implementation in yolov4
Import CV2 prompt importerror: libgl so. 1: Cannot open shared object file: no such file or directory
ModuleNotFoundError: No module named ‘picamera‘
Error: "mountvolume.setup failed for volume PVC fault handling
Binary search (half search)
C语言数组专题训练
Build a microservice cluster environment locally and learn to deploy automatically
About vscode, "code unreachable" will be displayed when calling sendline series functions with pwntools“
摄像头的MIPI接口、DVP接口和CSI接口
基于Cortex-M3、M4的GPIO口位带操作宏定义(可总线输入输出,可用于STM32、ADuCM4050等)
Xiaomi written test real question 1
GPIO port bit based on Cortex-M3 and M4 with operation macro definition (can be used for bus input and output, STM32, aducm4050, etc.)
mysql设置触发器问题
[software testing] 03 -- overview of software testing
Ros2 - configuration development environment (V)
小米笔试真题一
SOC_ SD_ CMD_ FSM
乐鑫面试流程
[tf] Unknown: Failed to get convolution algorithm. This is probably because cuDNN failed to initial
PHY drive commissioning --- mdio/mdc interface Clause 22 and 45 (I)