首先,DelayQueue是一个无界阻塞队列,只有在延迟期满时才能从中提取元素。可以设置该队列的存活时间、移除时间、唯一ID等元素。
DelayQueue阻塞队列在我们系统开发中也常常会用到,例如:延迟多久执行某操作;任务调度系统,能够准确的把握任务的执行时间。为了具有调用行为,存放到DelayDeque的元素必须继承Delayed接口。Delayed接口使对象成为延迟对象,它使存放在DelayQueue类中的对象具有了激活日期。
- CompareTo(Delayed o):Delayed接口继承了Comparable接口,因此有了这个方法。
- getDelay(TimeUnit unit):这个方法返回到激活日期的剩余时间,时间单位由单位参数指定。
- new DelayedItem(T t, Long liveTime, String id)--构造函数,之后附上代码。
public class DelayEvent<T extends Runnable> implements Delayed { private T t; private Long liveTime ; private Long removeTime; private String id; private static final AtomicLong atomic = new AtomicLong(0); public DelayEvent(T t,Long liveTime, String id){ this.setT(t); this.liveTime = liveTime; this.id = id; this.removeTime = removeTime ; } @Override public int compareTo(Delayed o) { if (o == null) return 1; if (o == this) return 0; if (o instanceof DelayEvent){ DelayEvent<T> tmpDelayEvent = (DelayEvent<T>)o; if (removeTime > tmpDelayEvent.removeTime ) { return 1; }else if (removeTime.equals(tmpDelayEvent.removeTime)) { return 0; }else { return -1; } } long diff = getDelay(TimeUnit.NANOSECONDS) - o.getDelay(TimeUnit.NANOSECONDS); return diff > 0 ? 1:diff == 0? 0:-1; } @Override public long getDelay(TimeUnit unit) { return unit.convert(removeTime - System.nanoTime(), unit); } public T getT() { return t; } public void setT(T t) { this.t = t; } @Override public int hashCode(){ return id.hashCode(); } @Override public boolean equals(Object object){ if (object instanceof DelayEvent) { DelayedItem o = (DelayEvent) object; return id != null && o.id != null && id.equals(o.id); } return false; } public Long getLiveTime() { return liveTime; } public Long getRemoveTime() { return removeTime; } public String getId() { return id; } public void setId(String id) { this.id = id; } }
操作计时队列(加入计时队列、移除计时队列等):
public interface TimeQueue<T extends Runnable> { /** * 新加入计时队列 * @param delayedEvent * @return */ boolean addQueue(DelayedEvent<T> delayedEvent); /** * 重新加入计时队列 * @param delayedEvent * @return */ boolean reAddQueue(DelayedEvent<T> delayedEvent); /** * 删除计时队列 * @param delayedEvent * @return */ boolean removeQueue(DelayedEvent<T> delayedEvent); /** * 是否包含 * @param delayedEvent * @return */ boolean contains(DelayedEvent<T> delayedEvent); int size(); }
接口实现类:
@Component public class TimeQueueImpl<T extends Runnable> implements TimeQueue<T> { public static final DelayQueue<DelayedEvent> timeQueue = new DelayQueue<DelayedEvent>(); @Override public boolean reAddQueue(DelayedEvent<T> delayedEvent) { removeQueue(delayedEvent); return addQueue(delayedEvent); } @Override public boolean addQueue(DelayedEvent<T> delayedEvent) { boolean addFlag = timeQueue.add(delayedEvent); return addFlag; } @Override public boolean removeQueue(DelayedEvent<T> delayedEvent) { boolean removeFlag = timeQueue.remove(delayedEvent); return removeFlag; } @Override public boolean contains(DelayedEvent<T> delayedEvent) { return timeQueue.contains(delayedItem); } @Override public int size() { return timeQueue.size(); } }
操作案例代码:
timeQueue.addQueue(new DelayedEvent(new delayQueueTask("此处需要传入用到的资源"), "需要触发的时间段", "唯一队列ID"); // 具体执行: public class DelayQueueTask implements Runnable { private AService aService; public DelayQueueTask(AService aService) { this.aService = aService; } @Override public void run() { try { aService.execute(); } catch (Exception e) { log.error("任务处理异常:{}", "Exception==", e); } } }