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. vi 配置

    vim       ~/.vimrc source    ~/.vimrc 添加相关配置 一直生效 0  行首 $    行尾 spacebar   空格键:         合并多行 spaceba ...

  2. getMemory的经典例子

    //NO.1:程序首先申请一个char类型的指针str,并把str指向NULL(即str里存的是NULL的地址,*str为NULL中的值为0),调用函数的过程中做了如下动作:1申请一个char类型的指 ...

  3. .NET 应用架构电子书中文版

    <.NET 微服务:容器化 .NET 应用架构指南> 本书主要介绍了基于容器和微服务的应用架构和设计原则,以及基于 .NET Core 和 Docker 容器的应用程序开发实践.为了让大家 ...

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

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

  5. [Swift]LeetCode401. 二进制手表 | Binary Watch

    A binary watch has 4 LEDs on the top which represent the hours (0-11), and the 6 LEDs on the bottom ...

  6. [Swift]LeetCode478. 在圆内随机生成点 | Generate Random Point in a Circle

    Given the radius and x-y positions of the center of a circle, write a function randPoint which gener ...

  7. [Swift]LeetCode517. 超级洗衣机 | Super Washing Machines

    You have n super washing machines on a line. Initially, each washing machine has some dresses or is ...

  8. 面试前必知Redis面试题—缓存雪崩+穿透+缓存与数据库双写一致问题

    今天来分享一下Redis几道常见的面试题: 如何解决缓存雪崩? 如何解决缓存穿透? 如何保证缓存与数据库双写时一致的问题? 一.缓存雪崩 1.1什么是缓存雪崩? 回顾一下我们为什么要用缓存(Redis ...

  9. APK安装成功后点击"打开",按Home键,在桌面点击图标后应用重启

    转载:http://blog.csdn.net/kepoon/article/details/7468688 问题: 安装系统SD卡里面的apk或者原有的程序更新版本的时候,会遇到升级安装成功之后的一 ...

  10. kubernetes进阶之七:Service

    1.概述 Service也是Kubernetes里的最核心的资源对象之一,Kubernetes里的每个Service其实就是我们经常提起的微服务架构中的一个“微服务”,之前我们所说的Pod.RC等资源 ...