当前位置:网站首页>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 :
边栏推荐
- Powermanagerservice (I) - initialization
- MySQL setting trigger problem
- Docker installs MySQL and uses Navicat to connect
- [software testing] 05 -- principles of software testing
- Ros2 - common command line (IV)
- mysql设置触发器问题
- PHY驱动调试之 --- MDIO/MDC接口22号和45号条款(一)
- The difference between new and malloc
- SD_CMD_RECEIVE_SHIFT_REGISTER
- M2dgr slam data set of multi-source and multi scene ground robot
猜你喜欢

Solve tensorfow GPU modulenotfounderror: no module named 'tensorflow_ core. estimator‘

1290_FreeRTOS中prvTaskIsTaskSuspended()接口实现分析

Ros2 topic (VIII)

PHY drive commissioning - phy controller drive (II)

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

Log4qt usage of logbase in QT project

C learning notes

SD_ CMD_ SEND_ SHIFT_ REGISTER

Ros2 - common command line (IV)

DelayQueue延迟队列的使用和场景
随机推荐
小米笔试真题一
Literacy Ethernet MII interface types Daquan MII, RMII, smii, gmii, rgmii, sgmii, XGMII, XAUI, rxaui
网易To B,柔外刚中
【软件测试】04 -- 软件测试与软件开发
The difference between new and malloc
SOC_ SD_ CMD_ FSM
Ros2 - configuration development environment (V)
iNFTnews | 喝茶送虚拟股票?浅析奈雪的茶“发币”
2022年中纪实 -- 一个普通人的经历
SOC_SD_DATA_FSM
Marvell 88e1515 PHY loopback mode test
现在有html文件,和用vs制作的mvc(连接了数据库),怎么两个相连?
基于Cortex-M3、M4的GPIO口位带操作宏定义(可总线输入输出,可用于STM32、ADuCM4050等)
[software testing] 04 -- software testing and software development
Netease to B, soft outside, hard in
PostMessage communication
乐鑫面试流程
Ros2 - common command line (IV)
[tf] Unknown: Failed to get convolution algorithm. This is probably because cuDNN failed to initial
Ros2 - ros2 vs. ros1 (II)