4. 带有延迟时间的Queue(DelayQueue)
package com.gf.conn013; import java.util.concurrent.DelayQueue; /**
* DelayQueue: 带有延迟时间的Queue,其中的元素只有当其指定的延迟时间到了,才能够从队列中获取改元素
* DelayQueue中的元素必须实现Delay接口,
* DelayQueue是一个没有大小限制的队列,
* 应用场景很多,比如对缓存超时的数据进行移除、任务超时处理、空闲连接的关闭等等
*
* @author huanchu
*
*/
public class WangBa implements Runnable{ private DelayQueue<Wangmin> queue = new DelayQueue<Wangmin>(); private boolean yinye = true; public void shangji(String name , String id , int money){
Wangmin man = new Wangmin(name , id , 1000 * money + System.currentTimeMillis()); System.out.println("网名"+man.getName() + " 身份证" + man.getId() + " 交钱" + money + "块 ,开始上机了..."); this.queue.add(man);
} public void xiaji(Wangmin man){
System.out.println("网民"+ man.getName() + "下机了...");
} @Override
public void run() {
while (yinye){
try {
Wangmin man = queue.take();
xiaji(man);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
} public static void main(String[] args) {
try {
System.out.println("网吧开始营业"); WangBa siyu = new WangBa(); new Thread(siyu).start(); siyu.shangji("路人甲", "123", 1);
siyu.shangji("路人乙", "234", 10);
siyu.shangji("路人丙", "345", 5);
siyu.shangji("路人丁", "456", 20); } catch (Exception e) {
e.printStackTrace();
}
} }
package com.gf.conn013; import java.util.concurrent.Delayed;
import java.util.concurrent.TimeUnit; public class Wangmin implements Delayed { private String name; private String id; private long endTime; private TimeUnit timeUnit = TimeUnit.SECONDS; public Wangmin(String name, String id, long endTime) {
this.name = name;
this.id = id;
this.endTime = endTime;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String getId() {
return id;
} public void setId(String id) {
this.id = id;
} public long getEndTime() {
return endTime;
} public void setEndTime(long endTime) {
this.endTime = endTime;
} public TimeUnit getTimeUnit() {
return timeUnit;
} public void setTimeUnit(TimeUnit timeUnit) {
this.timeUnit = timeUnit;
} @Override
public long getDelay(TimeUnit unit) { return endTime - System.currentTimeMillis();
} @Override
public int compareTo(Delayed delayed) { Wangmin w = (Wangmin) delayed; return this.getDelay(this.timeUnit) - w.getDelay(this.timeUnit) > 0 ? 1:0;
} }
关注我的公众号,精彩内容不能错过
4. 带有延迟时间的Queue(DelayQueue)的更多相关文章
- java线程安全之并发Queue
关闭 原 java线程安全之并发Queue(十三) 2017年11月19日 23:40:23 小彬彬~ 阅读数:12092更多 所属专栏: 线程安全 版权声明:本文为博主原创文章,未经博主允许不 ...
- 了解Queue
在并发队列上JDK提供了两套实现,一个是以ConcurrentLinkedQueue为代表的高性能队列,一个是以BlockingQueue接口为代表的阻塞队列,无论哪种都继承自Queue, 可以对应着 ...
- 并发queue
在并发队列上JDK提供了两套实现,一个是以ConcurrentLinkedQueue为代表的高性能队列,一个是以BlockingQueue接口为代表的阻塞队列,无论哪种都继承自Queue. 一.Con ...
- 【java】Java多线程总结之线程安全队列Queue【转载】
原文地址:https://www.cnblogs.com/java-jun-world2099/articles/10165949.html ============================= ...
- java 线程安全并发Queue
并发Queue 在并发的队列上jdk提供了两套实现,一个是以ConcurrentLinkedQueue为代表的高性能队列,一个是以BlockingQueue接口为代表的阻塞队列,无论在那种都继承自Qu ...
- 同步类容器和并发类容器——ConcurrentMap、CopyOnWrite、Queue
一 同步类容器同步类容器都是线程安全的,但在某些场景中可能需要加锁来保证复合操作. 符合操作如:迭代(反复访问元素,遍历完容器中所有元素).跳转(根据指定的顺序找到当前元素的下一个元素).条件运算. ...
- 并发队列 ConcurrentLinkedQueue 及 BlockingQueue 接口实现的四种队列
队列是一种特殊的线性表,它只允许在表的前端(front)进行删除操作,而在表的后端(rear)进行插入操作.进行插入操作的端称为队尾,进行删除操作的端称为队头.队列中没有元素时,称为空队列. 在队列这 ...
- java.util.concurrent.DelayQueue 源码学习
jdk1.8 DelayQueue,带有延迟元素的线程安全队列,当非阻塞从队列中获取元素时,返回最早达到延迟时间的元素,或空(没有元素达到延迟时间).DelayQueue的泛型参数需要实现Delaye ...
- 【Java并发编程】19、DelayQueue源码分析
DelayQueue,带有延迟元素的线程安全队列,当非阻塞从队列中获取元素时,返回最早达到延迟时间的元素,或空(没有元素达到延迟时间).DelayQueue的泛型参数需要实现Delayed接口,Del ...
随机推荐
- Expedition---POJ - 2431
A group of cows grabbed a truck and ventured on an expedition deep into the jungle. Being rather poo ...
- Jmeter中连接Oracle报错Cannot create PoolableConnectionFactory
填坑贴,之前一直用jmeter2.13版本进行oracle测试,今天改为3.2版本,发现按照以往的方法执行测试,JDBC Request结果始终报错:Cannot create PoolableCon ...
- C# 使用NPOI 处理Excel(Datable与Excel相互转换)
VS上有自带的程序集可以读取,但是总是会出现这样或那样的问题,让人恨得牙疼!而且效率太慢了.用NPOI就好多了,比较快,而且稳定,还简单,引用相应的程序集就好了. Excel转换成Datable pr ...
- Devexpress Ribbon 动态生成菜单
/// <summary> /// 动态加载菜单 /// </summary> private void GetMenuBind() { //根据登录用户角色菜单动态创建 // ...
- mongodb怎么创建数据库和配置用户
mongodb怎么创建数据库和配置用户,远程连接是不够的,还要上去操作和放数据的. 系统:centos 5.x 环境:mongodb 1.安装mongodb 这步就不说了,大家自己去看Centos安装 ...
- 【阿里聚安全·安全周刊】女主换脸人工合成小电影|伊朗间谍APP苹果安卓皆中招
本周的七个关键词: 人工智能 丨 HTTP链接=不安全链接 丨 滑动验证码 丨 伊朗间谍APP 丨 加密挖矿 丨 Android应用测试速查表 丨 黑客销售签名证书 -1- [人工智能]女主换 ...
- SSL/TLS握手过程
----------------------------------专栏导航----------------------------------HTTPS协议详解(一):HTTPS基础知识 HTTPS ...
- 事件派发dispatchEvent
1.什么是dispatchEvent? dispatch意为"调度"."派遣",event为"事件".所以dispatchEvent即向指定 ...
- [Swift]LeetCode687. 最长同值路径 | Longest Univalue Path
Given a binary tree, find the length of the longest path where each node in the path has the same va ...
- [Swift]LeetCode883. 三维形体投影面积 | Projection Area of 3D Shapes
On a N * N grid, we place some 1 * 1 * 1 cubes that are axis-aligned with the x, y, and z axes. Each ...