JAVA集合 DelayQueue 的使用 (同步的超时队列)
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了JAVA集合 DelayQueue 的使用 (同步的超时队列),小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3976字,纯文字阅读大概需要6分钟。
内容图文
![JAVA集合 DelayQueue 的使用 (同步的超时队列)](/upload/InfoBanner/zyjiaocheng/1106/e8eaead2b6cd42f18b2cb9e816e6c053.jpg)
https://www.cnblogs.com/hhan/p/10678466.html
展开 文章目录 [一] 简介 [二] DelayQueue 继承体系 1. 核心方法 take() put(E) offer(E) poll() peek() size() clear() [三] 使用 DelayQueue 准备 Delayed 的实现类 1. 构造方法 [一] 简介 注释来自java api Delayed 元素的一个无界阻塞队列,只有在延迟期满时才能从中提取元素。该队列的头部 是延迟期满后保存时间最长的 Delayed 元素。如果延迟都还没有期满,则队列没有头部,并且 poll 将返回 null。当一个元素的 getDelay(TimeUnit.NANOSECONDS) 方法返回一个小于等于 0 的值时,将发生到期。即使无法使用 take 或 poll 移除未到期的元素,也不会将这些元素作为正常元素对待。例如,size 方法同时返回到期和未到期元素的计数。此队列不允许使用 null 元素。 [二] DelayQueue 继承体系 publicclass DelayQueue<E extends Delayed>extends AbstractQueue<E>implements BlockingQueue<E> 1 1. 核心方法 take() 获取并移除此队列的头部,在可从此队列获得到期延迟的元素之前一直等待(如有必要)。 put(E) 将指定元素插入此延迟队列。 offer(E) 将指定元素插入此延迟队列。 poll() 获取并移除此队列的头,如果此队列不包含具有已到期延迟时间的元素,则返回 null。 peek() 获取但不移除此队列的头部;如果此队列为空,则返回 null。 size() 返回此 collection 中的元素数。 clear() 自动移除此延迟队列的所有元素。 [三] 使用 DelayQueue 准备 Delayed 的实现类 DelayQueue<E extends Delayed> 使用这个超时队列, 根据他的泛型限制类型, 他所存储的元素必须是 Delayed 这个接口的子类; 因此我们必须先构造一个这样的子类, 假设有一个定时任务 Task, 在设定时间然后从队列取出执行他. 定时任务 Task /** * * 定时任务 */publicclass Task { // 任务idprivate Integer id; // 任务名称private String name; // 执行时间private Long time; public Task(Integer id, String name, Long time) { this.id = id; this.name = name; this.time = time; } public Integer getId() { return id; } publicvoid setId(Integer id) { this.id = id; } public String getName() { return name; } publicvoid setName(String name) { this.name = name; } public Long getTime() { return time; } publicvoid setTime(Long time) { this.time = time; } @Override public String toString() { return "Task [id=" + id + ", name=" + name + ", time=" + new Date(time).toString() + "]"; } } Delayed 实现类: 因为我们并不想改变 Task 的结构, 所以另外创建一个实现类 TaskDelayed publicclass TaskDelayed implements Delayed { // 任务private Task task; public TaskDelayed(Task task) { super(); this.task = task; } public Task getTask() { return task; } @Override publicint compareTo(Delayed o) { return (int) (this.getDelay(TimeUnit.MILLISECONDS) - o.getDelay(TimeUnit.MILLISECONDS)); } @Override publiclong getDelay(TimeUnit unit) { long time = task.getTime(); long currentTime = System.currentTimeMillis(); return unit.convert(time - currentTime, TimeUnit.MILLISECONDS); } } 1. 构造方法 BlockingQueue<TaskDelayed> queue = new DelayQueue<>(); 1 put 与 take (一直阻塞直到完成操作为止) publicstaticvoid main(String[] args) throws InterruptedException { BlockingQueue<TaskDelayed> queue = new DelayQueue<>(); queue.put(new TaskDelayed(new Task(5, "ssss", System.currentTimeMillis() + 9000L))); queue.put(new TaskDelayed(new Task(2, "ssss", System.currentTimeMillis() + 6000L))); queue.put(new TaskDelayed(new Task(3, "ssss", System.currentTimeMillis() + 7000L))); queue.put(new TaskDelayed(new Task(1, "ssss", System.currentTimeMillis() + 5000L))); queue.put(new TaskDelayed(new Task(4, "ssss", System.currentTimeMillis() + 8000L))); for(;;) { System.out.println(queue.take().getTask() + "----" + new Date()); } } 执行结果 Task [id=1, name=ssss, time=Wed Jan 23 14:09:12 CST 2019]----Wed Jan 23 14:09:12 CST 2019 Task [id=2, name=ssss, time=Wed Jan 23 14:09:13 CST 2019]----Wed Jan 23 14:09:13 CST 2019 Task [id=3, name=ssss, time=Wed Jan 23 14:09:14 CST 2019]----Wed Jan 23 14:09:14 CST 2019 Task [id=4, name=ssss, time=Wed Jan 23 14:09:15 CST 2019]----Wed Jan 23 14:09:15 CST 2019 Task [id=5, name=ssss, time=Wed Jan 23 14:09:16 CST 2019]----Wed Jan 23 14:09:16 CST 2019 这里取出的时间和系统当前时间一致, 因为我们指定了他取出的时间 关键方法在于 Delayed publicinterface Delayed extends Comparable<Delayed> { long getDelay(TimeUnit unit); } 具体实现: 返回与此对象相关的剩余延迟时间,以给定的时间单位表示。 @Override publiclong getDelay(TimeUnit unit) { long time = task.getTime(); long currentTime = System.currentTimeMillis(); return unit.convert(time - currentTime, TimeUnit.MILLISECONDS); } ———————————————— 版权声明:本文为CSDN博主「程序猿-Jojo」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。 原文链接:https://blog.csdn.net/weixin_39554102/article/details/86608830
原文:https://www.cnblogs.com/kelelipeng/p/12721510.html
内容总结
以上是互联网集市为您收集整理的JAVA集合 DelayQueue 的使用 (同步的超时队列)全部内容,希望文章能够帮你解决JAVA集合 DelayQueue 的使用 (同步的超时队列)所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。