当前位置:网站首页>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 :
边栏推荐
- 【软件测试】04 -- 软件测试与软件开发
- Ros2 - common command line (IV)
- window navicat连接阿里云服务器mysql步骤及常见问题
- *P++, (*p) + +, * (p++) differences
- ModuleNotFoundError: No module named ‘picamera‘
- inux摄像头(mipi接口)简要说明
- PHY drive commissioning --- mdio/mdc interface Clause 22 and 45 (I)
- Intelligent target detection 59 -- detailed explanation of pytoch focal loss and its implementation in yolov4
- Netease to B, soft outside, hard in
- 2022年中纪实 -- 一个普通人的经历
猜你喜欢
U-Boot初始化及工作流程分析
Ret2xx---- common CTF template proposition in PWN
[software testing] 04 -- software testing and software development
PostMessage communication
Three body goal management notes
Mathematical analysis_ Notes_ Chapter 8: multiple integral
Ros2 - Service Service (IX)
DataGrid offline installation of database driver
ROS2——Service服务(九)
Build a microservice cluster environment locally and learn to deploy automatically
随机推荐
SD_CMD_SEND_SHIFT_REGISTER
U-Boot初始化及工作流程分析
1290_FreeRTOS中prvTaskIsTaskSuspended()接口实现分析
[node] NVM version management tool
Implementation of one-dimensional convolutional neural network CNN based on FPGA (VIII) implementation of activation layer
Now there are HTML files and MVC made with vs (connected to the database). How can they be connected?
【软件测试】06 -- 软件测试的基本流程
Ros2 - workspace (V)
The SQL implementation has multiple records with the same ID, and the latest one is taken
Log4qt usage of logbase in QT project
[framework] multi learner
Spinningup drawing curve
Steps and FAQs of connecting windows Navicat to Alibaba cloud server MySQL
[MySQL 8.0 does not support capitalization of table names - corresponding scheme]
ImportError: No module named ‘Tkinter‘
SD_ CMD_ SEND_ SHIFT_ REGISTER
你心目中的数据分析 Top 1 选 Pandas 还是选 SQL?
Application of MATLAB in Linear Algebra (4): similar matrix and quadratic form
Use of Pai platform
Empire help