DelayQueue的用法
2016-05-15 22:16
330 查看
package com.fit.test; import java.util.concurrent.DelayQueue; import java.util.concurrent.Delayed; import java.util.concurrent.TimeUnit; public class DelayTest { static class DelayAlarm implements Delayed { public String id; public long daltime; public long delay; public long time; public DelayAlarm ( String id ) { this.id = id; } public DelayAlarm ( String id, long daltime, long delay ) { this.id = id; this.daltime = daltime; this.delay = delay; this.time = this.daltime + this.delay; } /** * 预期时间在前的大:return 1 */ @Override public int compareTo( Delayed o ) { long result = ( (DelayAlarm) o ).getTime() - this.getTime(); if ( result < 0 ) return 1; if ( result > 0 ) return -1; return 0; } /** * 预期时间-当前时间 */ @Override public long getDelay( TimeUnit unit ) { return unit.convert( time * 1000L - System.currentTimeMillis(), TimeUnit.MILLISECONDS ); } /** * 根据流水号删除 */ @Override public boolean equals( Object o ) { if ( o == null ) return false; DelayAlarm a = (DelayAlarm) o; if ( null == this.getId() || null == a.getId() ) return false; return this.id.equals( a.getId() ); } public String getId() { return id; } public long getDaltime() { return daltime; } public long getDelay() { return delay; } public long getTime() { return time; } public String toString() { return "{id:" + id + ", daltime:" + daltime + ", delay:" + delay + " , time:" + time + "}"; } } //缓存队列 public static DelayQueue<DelayAlarm> queue = new DelayQueue<DelayAlarm>(); // public static volatile boolean stop; public static void push( DelayAlarm a ) { if ( !queue.offer( a ) ) { System.out.println( "/- push error." ); } } static class Consumer extends Thread { public void run() { while ( !stop ) { try { DelayAlarm a = queue.take(); System.out.println( System.currentTimeMillis() / 1000L + "----" + a ); } catch ( InterruptedException e ) { e.printStackTrace(); } } } } public static void main( String[] args ) { new Consumer().start(); long daltime = System.currentTimeMillis() / 1000; DelayAlarm a = new DelayAlarm( "001", daltime, 1L ); DelayAlarm b = new DelayAlarm( "002", daltime, 3L ); DelayAlarm c = new DelayAlarm( "003", daltime, 5L ); DelayAlarm d = new DelayAlarm( "004", daltime, 2L ); DelayAlarm f = new DelayAlarm( "005", daltime, 10L ); push( a ); push( b ); push( c ); push( d ); push( f ); //移除003的告警 DelayAlarm g = new DelayAlarm( "003" ); if ( queue.contains( g ) ) { queue.remove( g ); } } }
相关文章推荐
- Debian 6 Squeeze允许root登录
- APUE.2e程序清单4-7举例分析
- JAVA反编译器JD-GUI简介
- [leetcode] 232. Implement Queue using Stacks
- [PWA] 4. Hijacking Request
- 子序列的和(subsequence)
- 将一个CIImage转换为我们的UIImage
- 区间DP POJ 1141 Brackets Sequence
- UVA 1584 Circular Sequence【字典序】
- hzauoj Problem J: Arithmetic Sequence (dp)
- JAX-RS (REST Web Services) 2.0 requires Java 1.7 o
- android java.lang.IllegalStateException: trying to requery an already closed cursor
- 自定义UITabbarController及向UITabBar中添加自定义按钮
- HashMap根据value排序
- LintCode : Unique Paths II
- In-memory Computing with SAP HANA读书笔记 - 第七章:Business continuity and resiliency for SAP HANA
- XueXX and Chessboard(中南OJ)
- UI控件--自定义SeekBar样式
- LintCode : Unique Paths
- XueXX and P-P String(中南OJ)