BlockingQueue之DelayQueue的学习使用
DelayQueue 是一中阻塞队列,需要实现接口Delayed定义的方法.做下使用记录和心得吧,
@Data
public class DelayQueueExample implements Delayed { private String beginTime;
private String name; public static DateTimeFormatter dateTimeFormatter=DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
public LocalDateTime localDateTime; public DelayQueueExample(String beginTime, String name) {
this.beginTime = beginTime ;
this.name = name;
} @Override
public long getDelay(TimeUnit unit) { // TimeUnit.NANOSECONDS.convert(1000,TimeUnit.DAYS);
//long ss= unit.convert(afterTime - System.nanoTime(), TimeUnit.MILLISECONDS);
// System.out.println("getDelay()"+ss);
//unit.convert(afterTime - System.nanoTime(), TimeUnit.MILLISECONDS) //与当前时间对比,小于当前时间的话就进入队列,否则就抛弃这个队列的数据
localDateTime= LocalDateTime.parse(beginTime,dateTimeFormatter);
return localDateTime.toInstant(ZoneOffset.of("+8")).toEpochMilli()- System.currentTimeMillis();//是否小于当前时间 } @Override
public int compareTo(Delayed o) {
int temp = (int) (o.getDelay(TimeUnit.MILLISECONDS) - this.getDelay(TimeUnit.MILLISECONDS));
if (temp < 0) {
return 1;
}else {
return -1;
}
// return (int) (o.getDelay(TimeUnit.MILLISECONDS) - this.getDelay(TimeUnit.MILLISECONDS));
} @Override
public String toString() {
return "DelayQueueExample{" +
"beginTime=" +(beginTime) +
", name='" + name + '\'' +
'}';
} public static void main(String[] args) throws InterruptedException {
DelayQueue<DelayQueueExample> delayQueue = new DelayQueue<DelayQueueExample>();
delayQueue.add(new DelayQueueExample("2018-06-18 00:45:00", "hhe100"));
delayQueue.add(new DelayQueueExample("2018-06-18 00:47:00", "hhe101"));
delayQueue.add(new DelayQueueExample("2018-06-08 00:46:00", "hhe102"));
new Thread(new Runnable() {
@Override
public void run() {
while (true) {
DelayQueueExample element = null;
try {
element = delayQueue.poll();
if (element == null) {
break;
} else {
System.out.println(element);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}).start(); }
}
源码部分:取值的逻辑相似
public E poll() {
final ReentrantLock lock = this.lock;
lock.lock();
try {
E first = q.peek();
if (first == null || first.getDelay(NANOSECONDS) > 0)//丢掉延迟大于0的队列值
return null;
else
return q.poll();
} finally {
lock.unlock();
}
}
超时加排序机制
BlockingQueue之DelayQueue的学习使用的更多相关文章
- 多线程-BlockingQueue,Array[Linked]BlockingQueue,DelayQueue,PriorityBlockingQueue,SynchronousQueue
阻塞场景 BlockingQueue阻塞队列,阻塞的情况主要有如下2种: 1. 当队列满了,进行入队操作阻塞 2. 当队列空了,进行出队操作阻塞 阻塞队列主要用在生产者/消费者模式中,下图展示了一个线 ...
- DelayQueue使用
假设现有如下的使用场景: a) 关闭空闲连接.服务器中,有很多客户端的连接,空闲一段时间之后需要关闭之. b) 缓存.缓存中的对象,超过了空闲时间,需要从缓存中移出. c) 任务超时处理.在网络协议滑 ...
- DelayQueue详解
一.DelayQueue是什么 DelayQueue是一个无界的BlockingQueue,用于放置实现了Delayed接口的对象,其中的对象只能在其到期时才能从队列中取走.这种队列是有序的,即队头对 ...
- Java综合高级篇
1.你用过哪些集合类? 大公司最喜欢问的Java集合类面试题 40个Java集合面试问题和答案 java.util.Collections 是一个包装类.它包含有各种有关集合操作的静态多态方法. ja ...
- Java并发包线程池之ScheduledThreadPoolExecutor
前言 它是一种可以安排在给定的延迟之后执行一次或周期性执行任务的ThreadPoolExecutor.因为它继承了ThreadPoolExecutor, 当然也具有处理普通Runnable.Calla ...
- java并发6-小结
为什么需要并发 并发其实是一种解耦合的策略,它帮助我们把做什么(目标)和什么时候做(时机)分开.这样做可以明显改进应用程序的吞吐量(获得更多的CPU调度时间)和结构(程序有多个部分在协同工作).做 ...
- Java集合--线程安全(CopyOnWrite机制)
5 Java并发集合 5.1 引言 在前几章中,我们介绍了Java集合的内容,具体包括ArrayList.HashSet.HashMap.ArrayQueue等实现类. 不知道各位有没有发现,上述集合 ...
- [转载] java多线程学习-java.util.concurrent详解(四) BlockingQueue
转载自http://janeky.iteye.com/blog/770671 ------------------------------------------------------------- ...
- Java并发包源码学习系列:阻塞队列BlockingQueue及实现原理分析
目录 本篇要点 什么是阻塞队列 阻塞队列提供的方法 阻塞队列的七种实现 TransferQueue和BlockingQueue的区别 1.ArrayBlockingQueue 2.LinkedBloc ...
随机推荐
- 廖雪峰Java1-3流程控制-5循环
while循环 while循环首先判断条件: 条件满足时循环:条件不满足时退出循环 如果一开始条件就不满足,一次都不循环.如while false int sum = 0; int n = 1; wh ...
- VRRP、Track与NQA联动配置举例(Master监视上行链路)
原文 http://www.h3c.com/cn/d_201708/1018729_30005_0.htm#_Toc488338729 1. 组网需求 Host A需要访问Internet上的Host ...
- [UE4]Canvas Panel应用小技巧
当设置为满屏拉伸的时候,只要把“偏移左侧”和“偏移底部”都设置为0,就会自动拉伸为整屏了.再也不需要手动担心拉不满屏了.
- Go语言 数据类型,流程控制
Go语言 数据类型,流程控制 人生苦短,Let's Go ! package main // 必须要有一个main包 import "fmt" func main() { fmt. ...
- 第9章 应用层(2)_动态主机配置协议(DHCP)
2. 动态主机配置协议(DHCP) 2.1 静态地址和动态地址的应用场景 (1)静态地址应用场景 ①IP地址不经常更改的设备(如服务器地址) ②使用有规律的IP地址以便于管理(如学校机房为方便教师管理 ...
- redis管道技术
1.redis管道pipeline解决的问题: 由于redis通信是通过tcp协议基础,并且是堵塞的处理方式,在第一个请求没有执行并返回前,无法处理第二个请求.所以事件浪费在了网络传输和堵塞请求中. ...
- 【Selenium-WebDriver自学】Selenium-IDE测试创建(三)
==================================================================================================== ...
- StanFord ML 笔记 第四部分
第四部分: 1.生成学习法 generate learning algorithm 2.高斯判别分析 Gaussian Discriminant Analysis 3.朴素贝叶斯 Navie Baye ...
- Application failed to start because it could not find or load the QT platform plugin “windows”
只需要在.exe所在的目录下创建一个目录platforms添加进去缺失的dll即可如图: 参考链接:https://stackoverflow.com/questions/21268558/appli ...
- while循环、break、continue
我们通过while循环让python循环进行操作 break 跳出整个循环 continue 终止当前循环并不再继续往下执行,回到开头开始继续循环 下面会详细解释一下,例如: 1 a = 1 2 wh ...