当前位置:网站首页>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 :
边栏推荐
- Mipi interface, DVP interface and CSI interface of camera
- Binary search (half search)
- Lexin interview process
- 并发编程 — 如何中断/停止一个运行中的线程?
- [tf] Unknown: Failed to get convolution algorithm. This is probably because cuDNN failed to initial
- Empire help
- Volcano resource reservation feature
- 二分查找(折半查找)
- 纯碱是做什么的?
- About vscode, "code unreachable" will be displayed when calling sendline series functions with pwntools“
猜你喜欢
随机推荐
ROS2——功能包(六)
[tf] Unknown: Failed to get convolution algorithm. This is probably because cuDNN failed to initial
SOC_SD_CMD_FSM
ORACLE CREATE SEQUENCE,ALTER SEQUENCE,DROP SEQUENCE
The difference between NPM install -g/-save/-save-dev
npm install -g/--save/--save-dev的区别
1290_ Implementation analysis of prvtaskistasksuspended() interface in FreeRTOS
The differences and connections among cookies, sessions, JWT, and tokens
The SQL implementation has multiple records with the same ID, and the latest one is taken
ROS2——topic话题(八)
PostMessage communication
2022.06.27_ One question per day
[software testing] 02 -- software defect management
testing framework
你心目中的数据分析 Top 1 选 Pandas 还是选 SQL?
Ros2 - Service Service (IX)
[MySQL 8.0 does not support capitalization of table names - corresponding scheme]
1290_FreeRTOS中prvTaskIsTaskSuspended()接口实现分析
并发编程 — 死锁排查及处理
D2L installation
![[node] NVM version management tool](/img/26/f13a2451c2f177a86bcb2920936468.png)







