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 ...
随机推荐
- 『Hi,我是易建科技eKing Cloud!』
写在前面:这是我的第一篇自我介绍式文章,是对易建科技我所在云服务事业群的云平台产品和业务的总结和介绍.本文始发于 Linux宝库 公众号,这是原文链接.感谢公众号主人陈绪总,感谢公众号的编辑们!感谢易 ...
- openstack热添加磁盘
假定在虚拟机当中添加了磁盘,但是虚拟机没有识别出来:如何识别出来 可以使用命令 echo '- - -' >/sys/class/scsi_host/host0/scan 使用后就可以识别出来了 ...
- [UE4]Text Block文字字体偏移
这样看起来就像是真正的垂直居中对齐了.
- tf.nn.conv_2d
tf.nn.conv2d(input, filter, strides, padding, use_cudnn_on_gpu=None, name=None) 结果返回一个Tensor,这个输出,就是 ...
- JVM调优原理
JVM堆栈 栈是运行时的单位,而堆是存储的单位. 栈解决程序的运行问题,即程序如何执行,或者说如何处理数据:堆解决的是数据存储的问题,即数据怎么放.放在哪儿. 在Java中一个线程就会相应有一个线程栈 ...
- sas infile 控制导入长度
/*尝试使用infile解决uesrname的录入只能存储一单位的问题*/data TestPayRecord2;infile "D:\开发工具\购买记录表.csv" dlm='2 ...
- BCGcontrolBar(四) ListCtrl 操作输出显示
Ribbon 的效果主要为在可停靠面板上放置各种组件 具体操作为新建一个watchbar 然后在watchbar上加入 ListCtrl组件 效果如下 因为是在可停靠面板上生成的 因此可以随意拖动 具 ...
- JDBC连接数据库,结合DbUtil数据库连接工具类的使用
(以Mysql数据库为例) 第一步:在项目里配置数据库驱动 Build Path->configure Build Path ->Add Exteral JARs 将JDBC驱动包导 ...
- Windows安装部署Tensorflow过程中的问题处理
1 在配置Tensorflow过程中有一行命令是 python object_detection/builders/model_builder_test.py 过程中出现了 NameError: na ...
- MySQL存储过程 事务transaction
MySQL 中,单个 Store Procedure(SP) 不是原子操作,而 Oracle 则是原子的.如下的存储过程,即使语句2 失败,语句 1 仍然会被 commit 到数据库中: create ...