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)的更多相关文章

  1. java线程安全之并发Queue

    关闭 原 java线程安全之并发Queue(十三) 2017年11月19日 23:40:23 小彬彬~ 阅读数:12092更多 所属专栏: 线程安全    版权声明:本文为博主原创文章,未经博主允许不 ...

  2. 了解Queue

    在并发队列上JDK提供了两套实现,一个是以ConcurrentLinkedQueue为代表的高性能队列,一个是以BlockingQueue接口为代表的阻塞队列,无论哪种都继承自Queue, 可以对应着 ...

  3. 并发queue

    在并发队列上JDK提供了两套实现,一个是以ConcurrentLinkedQueue为代表的高性能队列,一个是以BlockingQueue接口为代表的阻塞队列,无论哪种都继承自Queue. 一.Con ...

  4. 【java】Java多线程总结之线程安全队列Queue【转载】

    原文地址:https://www.cnblogs.com/java-jun-world2099/articles/10165949.html ============================= ...

  5. java 线程安全并发Queue

    并发Queue 在并发的队列上jdk提供了两套实现,一个是以ConcurrentLinkedQueue为代表的高性能队列,一个是以BlockingQueue接口为代表的阻塞队列,无论在那种都继承自Qu ...

  6. 同步类容器和并发类容器——ConcurrentMap、CopyOnWrite、Queue

     一 同步类容器同步类容器都是线程安全的,但在某些场景中可能需要加锁来保证复合操作. 符合操作如:迭代(反复访问元素,遍历完容器中所有元素).跳转(根据指定的顺序找到当前元素的下一个元素).条件运算. ...

  7. 并发队列 ConcurrentLinkedQueue 及 BlockingQueue 接口实现的四种队列

    队列是一种特殊的线性表,它只允许在表的前端(front)进行删除操作,而在表的后端(rear)进行插入操作.进行插入操作的端称为队尾,进行删除操作的端称为队头.队列中没有元素时,称为空队列. 在队列这 ...

  8. java.util.concurrent.DelayQueue 源码学习

    jdk1.8 DelayQueue,带有延迟元素的线程安全队列,当非阻塞从队列中获取元素时,返回最早达到延迟时间的元素,或空(没有元素达到延迟时间).DelayQueue的泛型参数需要实现Delaye ...

  9. 【Java并发编程】19、DelayQueue源码分析

    DelayQueue,带有延迟元素的线程安全队列,当非阻塞从队列中获取元素时,返回最早达到延迟时间的元素,或空(没有元素达到延迟时间).DelayQueue的泛型参数需要实现Delayed接口,Del ...

随机推荐

  1. 【转】priority_queue优先队列

    转自:http://www.cppblog.com/shyli/archive/2007/04/06/21366.html http://www.cppblog.com/shyli/archive/2 ...

  2. Luogu P1894 [USACO4.2]The Perfect Stall

    传送门 是道绿题???二分图(网络流)不应该是蓝打底??? 这题浏览一遍就知道是二分图(网络流)算法喽,二分图代码太短,不想写(←这人???),所以就拿网络流练练手. 设源点S=0,汇点T=n+m+1 ...

  3. SSIS - 11.For循环容器

    一.For循环容器中的3个循环变量 For循环容器,类似于编程语言中的For,用于重复执行容器内的任务,直到条件返回为False.与编程语言类似,For循环容器也需要定义以下3种循环属性: 注: 必须 ...

  4. 机器学习之正则化(Regularization)

    1. The Problem of Overfitting 1 还是来看预测房价的这个例子,我们先对该数据做线性回归,也就是左边第一张图. 如果这么做,我们可以获得拟合数据的这样一条直线,但是,实际上 ...

  5. 搭建一个舒适的 .NET Core 开发环境

    最近,一直在往.Net Core上迁移,随着工作的深入,发现.Net Core比.Net Framework好玩多了.不过目前还在windows下开发,虽然VisualStudio是宇宙第一神器,但是 ...

  6. [BlueZ] 1、Download install and use the BlueZ and hcitool on PI 3B+

    星期日, 02. 九月 2018 11:58下午 - beautifulzzzz 1. Introduction Bluez is the default Bluetooth protocol sta ...

  7. 【安富莱】【RL-TCPnet网络教程】第11章 RL-TCPnet调试方法

    第11章      RL-TCPnet调试方法 本章节为大家讲解RL-TCPnet的调试方法,RL-TCPnet的调试功能其实就是通过串口打印实时监控运行状态.而且RL-TCPnet的调试设置比较简单 ...

  8. 配置Zookeeper、Dubbox

    CentOS的配置: 1.给CentOS安装Zookeeper: 网络配置成仅主机 上传tar.gz:比如用FTP tar -xvzf ... cd zookeeper mkdir data cd c ...

  9. [Swift]LeetCode14. 最长公共前缀 | Longest Common Prefix

    Write a function to find the longest common prefix string amongst an array of strings. If there is n ...

  10. [Swift]LeetCode389. 找不同 | Find the Difference

    Given two strings s and t which consist of only lowercase letters. String t is generated by random s ...