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. 【CSS 第六天】三种简历

    1.盒子模型 2.三种简历 利用float和CSS制作内容一致,但是样式不一致的三种简历. 代码 3.效果 style-1 3.2效果2 效果三

  2. C# WPF 使用委托修改UI控件

    近段时间在自学WPF,是一个完全不懂WPF的菜鸟,对于在线程中修改UI控件使用委托做一个记录,给自已以后查询也给需要的参考: 界面只放一个RichTextBox,在窗体启动时开起两个线程,调用两个函数 ...

  3. [Swift]LeetCode286. 墙和门 $ Walls and Gates

    You are given a m x n 2D grid initialized with these three possible values. -1 - A wall or an obstac ...

  4. [Swift]LeetCode424. 替换后的最长重复字符 | Longest Repeating Character Replacement

    Given a string that consists of only uppercase English letters, you can replace any letter in the st ...

  5. [Swift]LeetCode826. 安排工作以达到最大收益 | Most Profit Assigning Work

    We have jobs: difficulty[i] is the difficulty of the ith job, and profit[i] is the profit of the ith ...

  6. [Swift]LeetCode886. 可能的二分法 | Possible Bipartition

    Given a set of N people (numbered 1, 2, ..., N), we would like to split everyone into two groups of  ...

  7. [Swift]LeetCode919. 完全二叉树插入器 | Complete Binary Tree Inserter

    A complete binary tree is a binary tree in which every level, except possibly the last, is completel ...

  8. IDEA激活码(2019)

    如您激活出现问题,请点击这里加入:软件激活问题解决群 前言 IDEA已然成为我最热爱的一款编辑器,作为一个从Eclipse阵营转过来的coder,确实能感受到IDEA的强大,而我电脑桌面的Eclips ...

  9. Vue Route Building the UI back-end framework

    Vue 的 路由就像ASP.NET MVC路径相似. 路由定义文件也是js格式的,我们都将这些文件放入到src的route文件中. 后台框架主入口: <template> <div ...

  10. 说一说MVC的Authentication过滤(四)

    前沿: 一般情况下,在我们做访问权限管理的时候,会把用户的正确登录后的基本信息保存在Session中,以后用户每次请求页面或接口数据的时候,拿到 Session中存储的用户基本信息,查看比较他有没有登 ...