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 ...
随机推荐
- 第二章:安装zabbix过程
2.2 安装zabbix过程 2.2.1 安装方式选择 编译安装 (服务较多,环境复杂) yum安装(干净环境) 使用yum 需要镜像yum源 http://www.cnblogs.com/clsn/ ...
- [深度分析] Python Web 开发框架 Bottle
[深度分析] Python Web 开发框架 Bottle(这个真的他妈的经典!!!) 作者:lhf2009913 Bottle 是一个非常精致的WSGI框架,它提供了 Python Web开发中需要 ...
- time_base
From:http://blog.csdn.net/supermanwg/article/details/14521869 FFMPEG的很多结构中有AVRational time_base;这样的一 ...
- requests模块学习
- 基于如下5点展开requests模块的学习 什么是requests模块 requests模块是python中原生的基于网络请求的模块,其主要作用是用来模拟浏览器发起请求.功能强大,用法简洁高效.在 ...
- Tomcat+Nginx+Redis+MySQL实现反向代理、负载均衡、session共享
一.环境准备 时间同步 关闭防火墙 联通网络,配置yum源 软件包链接:https://pan.baidu.com/s/1qYbtpnQ 二.安装nginx 1.解决依赖关系 [root@nginx- ...
- 30 个 OpenStack 经典面试问题和解答
现在,大多数公司都试图将它们的 IT 基础设施和电信设施迁移到私有云, 如 OpenStack.如果你打算面试 OpenStack 管理员这个岗位,那么下面列出的这些面试问题可能会帮助你通过面试.-- ...
- RecyclerView实现ViewPager效果;
看代码就好了,RecyclerView实现Viewpager的效果,添加了界面的改变监听,用法和普通的RecyclerView一样,还可以设置一次滑动多个界面: public class VpRecy ...
- String.prototype.normalize()
normalize() 方法会按照指定的一种 Unicode 正规形式将当前字符串正规化. 这是一个ES6方法. 许多欧洲语言有语调符号和重音符号.为了表示它们,Unicode 提供了两种方法.一种是 ...
- MySQL表中的数据类型
数据类型:在表中数据类型主要是限制字段必须以什么样的数据类型传值. 一 整型 整数类型:TINYINT SMALLINT MEDIUMINT INT BIGINT总共有五种,name我们一般用到的也就 ...
- 【Selenium-WebDriver自学】出现的问题和解决方案(十七)
==================================================================================================== ...