阻塞队列之七:DelayQueue延时队列
一、DelayQueue簡介
是一個無界的BlockingQueue,用于放置實現了Delayed接口的對象,其中的對象只能在其到期時才能從隊列中取走。這種隊列是有序的(PriorityQueue實際存放Delayed接口對象),即隊頭對象的延遲到期時間最短(隊列頂端總是最小的元素)。注意:不能將null元素放置到這種隊列中。
DelayQueue在poll/take的時候,隊列中元素會判定這個elment有沒有達到超時時間,如果沒有達到,poll返回null,而take進入等待狀態。但是,除了這兩個方法,隊列中的元素會被當做正常的元素來對待。例如,size方法返回所有元素的數量,而不管它們有沒有達到超時時間。而協調的Condition available只對take和poll是有意義的。
二、DelayQueue源碼分析
2.1、DelayQueue的lock
DelayQueue使用一個可重入鎖和這個鎖生成的一個條件對象進行并發控制。
private final transient ReentrantLock lock = new ReentrantLock();//內部用于存儲對象private final PriorityQueue<E> q = new PriorityQueue<E>();/*** Thread designated to wait for the element at the head of* the queue. This variant of the Leader-Follower pattern* (http://www.cs.wustl.edu/~schmidt/POSA/POSA2/) serves to* minimize unnecessary timed waiting. When a thread becomes* the leader, it waits only for the next delay to elapse, but* other threads await indefinitely. The leader thread must* signal some other thread before returning from take() or* poll(...), unless some other thread becomes leader in the* interim. Whenever the head of the queue is replaced with* an element with an earlier expiration time, the leader* field is invalidated by being reset to null, and some* waiting thread, but not necessarily the current leader, is* signalled. So waiting threads must be prepared to acquire* and lose leadership while waiting.*/private Thread leader = null;/*** Condition signalled when a newer element becomes available* at the head of the queue or a new thread may need to* become leader.*/private final Condition available = lock.newCondition();
2.2、成員變量
要先了解下DelayQueue中用到的幾個關鍵對象:
2.2.1、Delayed,?一種混合風格的接口,用來標記那些應該在給定延遲時間之后執行的對象。
此接口的實現必須定義一個?compareTo()方法,該方法提供與此接口的?getDelay()方法一致的排序。
public class DelayQueue<E extends Delayed> extends AbstractQueue<E>implements BlockingQueue<E> {DelayQueue是一個BlockingQueue,其泛型類的參數是Delayed接口對象。
Delayed接口:
public interface Delayed extends Comparable<Delayed> {long getDelay(TimeUnit unit); //返回與此對象相關的剩余延遲時間,以給定的時間單位表示。 }Comparable接口:
public interface Comparable<T> {public int compareTo(T o); }Delayed擴展了Comparable接口,比較的基準為延時的時間值,Delayed接口的實現類getDelay的返回值應為固定值(final)。
2.2.2、PriorityQueue,優先級隊列存放有序對象
優先隊列的比較基準值是時間。詳解見《阻塞隊列之八:PriorityBlockingQueue優先隊列》
DelayQueue的關鍵元素BlockingQueue、PriorityQueue、Delayed。可以這么說,DelayQueue是一個使用優先隊列(PriorityQueue)實現的BlockingQueue,優先隊列的比較基準值是時間。
public class DelayQueue<E extends Delayed> implements BlockingQueue<E> { private final PriorityQueue<E> q = new PriorityQueue<E>(); }總結:DelayQueue內部是使用PriorityQueue實現的,DelayQueue = BlockingQueue +?PriorityQueue + Delayed。
2.3、構造函數
public DelayQueue() {} public DelayQueue(Collection<? extends E> c) {this.addAll(c);} public boolean offer(E e, long timeout, TimeUnit unit) {return offer(e);}超時的參數被忽略,因為是無界的。不會阻塞或超時。
2.4、入隊
public boolean add(E e) {return offer(e);}public void put(E e) {offer(e);}public boolean offer(E e) {final ReentrantLock lock = this.lock;lock.lock();try {q.offer(e);if (q.peek() == e) {//添加元素后peek還是e,重置leader,通知條件隊列?leader = null;available.signal();}return true;} finally {lock.unlock();}}2.5、出隊
public E poll() { final ReentrantLock lock = this.lock; lock.lock(); try { E first = q.peek(); if (first == null || first.getDelay(TimeUnit.NANOSECONDS) > 0) //隊列為空或者延遲時間未過期 return null; else return q.poll(); } finally { lock.unlock(); } } /** * take元素,元素未過期需要阻塞 */ public E take() throws InterruptedException { final ReentrantLock lock = this.lock; lock.lockInterruptibly(); try { for (;;) { E first = q.peek(); if (first == null) available.await(); //隊列空,加入條件隊列 else { long delay = first.getDelay(TimeUnit.NANOSECONDS); //獲取剩余延遲時間 if (delay <= 0) //小于0,那就poll元素 return q.poll(); else if (leader != null) //有延遲,檢查leader,不為空說明有其他線程在等待,那就加入條件隊列 available.await(); else { Thread thisThread = Thread.currentThread(); leader = thisThread; //設置當前為leader等待 try { available.awaitNanos(delay); //條件隊列等待指定時間 } finally { if (leader == thisThread) //檢查是否被其他線程改變,沒有就重置,再次循環 leader = null; } } } } } finally { if (leader == null && q.peek() != null) //leader為空并且隊列不空,說明沒有其他線程在等待,那就通知條件隊列 available.signal(); lock.unlock(); } } /** * 響應超時的poll */ public E poll(long timeout, TimeUnit unit) throws InterruptedException { long nanos = unit.toNanos(timeout); final ReentrantLock lock = this.lock; lock.lockInterruptibly(); try { for (;;) { E first = q.peek(); if (first == null) { if (nanos <= 0) return null; else nanos = available.awaitNanos(nanos); } else { long delay = first.getDelay(TimeUnit.NANOSECONDS); if (delay <= 0) return q.poll(); if (nanos <= 0) return null; if (nanos < delay || leader != null) nanos = available.awaitNanos(nanos); else { Thread thisThread = Thread.currentThread(); leader = thisThread; try { long timeLeft = available.awaitNanos(delay); nanos -= delay - timeLeft; } finally { if (leader == thisThread) leader = null; } } } } } finally { if (leader == null && q.peek() != null) available.signal(); lock.unlock(); } } /** * 獲取queue[0],peek是不移除的 */ public E peek() { final ReentrantLock lock = this.lock; lock.lock(); try { return q.peek(); } finally { lock.unlock(); } }三、JDK或開源框架中使用
ScheduledThreadPoolExecutor中使用了DelayedWorkQueue。
應用場景
下面的應用場景是來源于網上,雖然借用DelayedQueue可以快速找到要“失效”的對象,但DelayedQueue內部的PriorityQueue的(插入、刪除時的排序)也耗費資源。
a) 關閉空閑連接。服務器中,有很多客戶端的連接,空閑一段時間之后需要關閉之。
b) 緩存。緩存中的對象,超過了空閑時間,需要從緩存中移出。
c) 任務超時處理。在網絡協議滑動窗口請求應答式交互時,處理超時未響應的請求。
d)session超時管理,網絡應答通訊協議的請求超時處理。
?
四、示例
1、緩存示例
以下是Sample,是一個緩存的簡單實現。共包括三個類Pair、DelayItem、Cache。如下:
package com.dxz.concurrent.delayqueue;public class Pair<K, V> {public K key;public V value;public Pair() {}public Pair(K first, V second) {this.key = first;this.value = second;}@Overridepublic String toString() {return "Pair [key=" + key + ", value=" + value + "]";}}以下是Delayed的實現
package com.dxz.concurrent.delayqueue;import java.util.concurrent.Delayed; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicLong;public class DelayItem<T> implements Delayed {/** Base of nanosecond timings, to avoid wrapping */private static final long NANO_ORIGIN = System.nanoTime();/*** Returns nanosecond time offset by origin*/final static long now() {return System.nanoTime() - NANO_ORIGIN;}/*** Sequence number to break scheduling ties, and in turn to guarantee FIFO* order among tied entries.*/private static final AtomicLong sequencer = new AtomicLong(0);/** Sequence number to break ties FIFO */private final long sequenceNumber;/** The time the task is enabled to execute in nanoTime units */private final long time;private final T item;public DelayItem(T submit, long timeout) {this.time = now() + timeout;this.item = submit;this.sequenceNumber = sequencer.getAndIncrement();}public T getItem() {return this.item;}public long getDelay(TimeUnit unit) {long d = unit.convert(time - now(), TimeUnit.NANOSECONDS);return d;}public int compareTo(Delayed other) {if (other == this) // compare zero ONLY if same objectreturn 0;if (other instanceof DelayItem) {DelayItem x = (DelayItem) other;long diff = time - x.time;if (diff < 0)return -1;else if (diff > 0)return 1;else if (sequenceNumber < x.sequenceNumber)return -1;elsereturn 1;}long d = (getDelay(TimeUnit.NANOSECONDS) - other.getDelay(TimeUnit.NANOSECONDS));return (d == 0) ? 0 : ((d < 0) ? -1 : 1);} }以下是Cache的實現,包括了put和get方法,還包括了可執行的main函數。
package com.dxz.concurrent.delayqueue;import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; import java.util.concurrent.DelayQueue; import java.util.concurrent.TimeUnit; import java.util.logging.Level; import java.util.logging.Logger;public class Cache<K, V> {private static final Logger LOG = Logger.getLogger(Cache.class.getName());private ConcurrentMap<K, V> cacheObjMap = new ConcurrentHashMap<K, V>();private DelayQueue<DelayItem<Pair<K, V>>> q = new DelayQueue<DelayItem<Pair<K, V>>>();private Thread daemonThread;public Cache() {Runnable daemonTask = new Runnable() {public void run() {daemonCheck();}};daemonThread = new Thread(daemonTask);daemonThread.setDaemon(true);daemonThread.setName("Cache Daemon");daemonThread.start();}private void daemonCheck() {if (LOG.isLoggable(Level.INFO))LOG.info("cache service started.");for (;;) {try {DelayItem<Pair<K, V>> delayItem = q.take();if (delayItem != null) {// 超時對象處理Pair<K, V> pair = delayItem.getItem();cacheObjMap.remove(pair.key, pair.value); // compare and// remove }} catch (InterruptedException e) {if (LOG.isLoggable(Level.SEVERE))LOG.log(Level.SEVERE, e.getMessage(), e);break;}}if (LOG.isLoggable(Level.INFO))LOG.info("cache service stopped.");}// 添加緩存對象public void put(K key, V value, long time, TimeUnit unit) {V oldValue = cacheObjMap.put(key, value);if (oldValue != null) {boolean result = q.remove(new DelayItem<Pair<K, V>>(new Pair<K, V>(key, oldValue), 0L));System.out.println("remove:="+result);}long nanoTime = TimeUnit.NANOSECONDS.convert(time, unit);q.put(new DelayItem<Pair<K, V>>(new Pair<K, V>(key, value), nanoTime));}public V get(K key) {return cacheObjMap.get(key);}public DelayQueue<DelayItem<Pair<K, V>>> getQ() {return q;}public void setQ(DelayQueue<DelayItem<Pair<K, V>>> q) {this.q = q;}// 測試入口函數public static void main(String[] args) throws Exception {Cache<Integer, String> cache = new Cache<Integer, String>();cache.put(1, "aaaa", 60, TimeUnit.SECONDS);cache.put(1, "aaaa", 10, TimeUnit.SECONDS);//cache.put(1, "ccc", 60, TimeUnit.SECONDS);cache.put(2, "bbbb", 30, TimeUnit.SECONDS);cache.put(3, "cccc", 66, TimeUnit.SECONDS);cache.put(4, "dddd", 54, TimeUnit.SECONDS);cache.put(5, "eeee", 35, TimeUnit.SECONDS);cache.put(6, "ffff", 38, TimeUnit.SECONDS);cache.put(1, "aaaa", 70, TimeUnit.SECONDS);for(;;) {Thread.sleep(1000 * 2);{for(Object obj : cache.getQ().toArray()) {System.out.print(((DelayItem)obj).toString());System.out.println(",");}System.out.println();}}} }結果片段1:(重復key的Delayed對象將從DelayedQueue中移除)
remove:=true remove:=true 七月 04, 2017 11:28:36 上午 com.dxz.concurrent.delayqueue.Cache daemonCheck 信息: cache service started. DelayItem [sequenceNumber=3, time=30000790187, item=Pair [key=2, value=bbbb]], DelayItem [sequenceNumber=6, time=35000842411, item=Pair [key=5, value=eeee]], DelayItem [sequenceNumber=7, time=38000847189, item=Pair [key=6, value=ffff]], DelayItem [sequenceNumber=5, time=54000835925, item=Pair [key=4, value=dddd]], DelayItem [sequenceNumber=4, time=66000803499, item=Pair [key=3, value=cccc]], DelayItem [sequenceNumber=9, time=70000900437, item=Pair [key=1, value=aaaa]],結果片段2:(隊頭對象將最先過時,可以被take()出來,這段代碼在daemonCheck()方法中,即對超時對象的處理,如這里是清理session集合對象)
... DelayItem [sequenceNumber=3, time=30000665600, item=Pair [key=2, value=bbbb]], DelayItem [sequenceNumber=6, time=35000689152, item=Pair [key=5, value=eeee]], DelayItem [sequenceNumber=7, time=38000694272, item=Pair [key=6, value=ffff]], DelayItem [sequenceNumber=5, time=54000685398, item=Pair [key=4, value=dddd]], DelayItem [sequenceNumber=4, time=66000679595, item=Pair [key=3, value=cccc]], DelayItem [sequenceNumber=9, time=70000728406, item=Pair [key=1, value=aaaa]],DelayItem [sequenceNumber=6, time=35000689152, item=Pair [key=5, value=eeee]], DelayItem [sequenceNumber=5, time=54000685398, item=Pair [key=4, value=dddd]], DelayItem [sequenceNumber=7, time=38000694272, item=Pair [key=6, value=ffff]], DelayItem [sequenceNumber=9, time=70000728406, item=Pair [key=1, value=aaaa]], DelayItem [sequenceNumber=4, time=66000679595, item=Pair [key=3, value=cccc]], ...?
?
總結
以上是生活随笔為你收集整理的阻塞队列之七:DelayQueue延时队列的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 学习《机器学习100天》第27天 什么是
- 下一篇: writer在java中的意思_Java