zoukankan      html  css  js  c++  java
  • DelayQueue

    /**
     *    DelayQueue 底层包含一个PriorityQueue。
     *    向DelayQueue插入的元素必须实现delay接口,DelayQueue是不边界的
     *   只有当队列中的元素过期了,即getDalay方法返回值小于0,元素才可以取出来
     *   当元素过期了不取出来,不会被删除,元素仍然在队列里
     */
    public class DelayQueueTest {
    
    
        public static void main(String[] args) throws InterruptedException {
            DelayQueue<Element<String>> queue = new DelayQueue<Element<String>>();
            queue.put(new Element<>("hello1", 3000));
            long start = System.currentTimeMillis();
            System.out.println("=========");
            // TimeUnit.SECONDS.sleep(5);  //验证元素过期了不取出来,不会被删除
            queue.take();
            System.out.println(System.currentTimeMillis()-start);
        }
    
    
    
        static  class  Element<E>   implements Delayed{
    
            final  E e;
    
            long expireTime;
            Element (E e , long  expireTime){
                this.e = e;
                this.expireTime = System.currentTimeMillis() + expireTime;
            }
    
    
            @Override
            public long getDelay(TimeUnit unit) {
                return expireTime - System.currentTimeMillis();
            }
    
            @Override
            public int compareTo(Delayed o) {
                Element that = (Element) o;
                if(this.expireTime < that.expireTime){
                    return  -1;
                }else if(this.expireTime > that.expireTime){
                    return  1;
                }else{
                    return 0;
                }
            }
        }
    }
  • 相关阅读:
    codevs1028 花店橱窗布置
    bzoj1497 [NOI2006]最大获利 (最大闭合权图)
    扩展欧几里得算法总结
    codevs1033 蚯蚓的游戏问题
    bzoj1001狼抓兔子
    CSS3中的选择器
    LESS快速入门
    怎样让老浏览器兼容html5新标签
    http与https差异
    css3中的radius
  • 原文地址:https://www.cnblogs.com/moris5013/p/12051330.html
Copyright © 2011-2022 走看看