<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>


    我們談一下實際的場景吧。我們在開發中,有如下場景

    a) 關閉空閑連接。服務器中,有很多客戶端的連接,空閑一段時間之后需要關閉之。
    b) 緩存。緩存中的對象,超過了空閑時間,需要從緩存中移出。
    c) 任務超時處理。在網絡協議滑動窗口請求應答式交互時,處理超時未響應的請求。

    一種笨笨的辦法就是,使用一個后臺線程,遍歷所有對象,挨個檢查。這種笨笨的辦法簡單好用,但是對象數量過多時,可能存在性能問題,檢查間隔時間不好設置,間隔時間過大,影響精確度,多小則存在效率問題。而且做不到按超時的時間順序處理。

    這場景,使用DelayQueue最適合了。

    DelayQueue 是java.util.concurrent中提供的一個很有意思的類。很巧妙,非常棒!但是java doc和Java SE 5.0的source中都沒有提供Sample。我最初在閱讀ScheduledThreadPoolExecutor源碼時,發現DelayQueue 的妙用。隨后在實際工作中,應用在session超時管理,網絡應答通訊協議的請求超時處理。

    本文將會對DelayQueue做一個介紹,然后列舉應用場景。并且提供一個Delayed接口的實現和Sample代碼。

    DelayQueue是一個BlockingQueue,其特化的參數是Delayed。(不了解BlockingQueue的同學,先去了解BlockingQueue再看本文)
    Delayed擴展了Comparable接口,比較的基準為延時的時間值,Delayed接口的實現類getDelay的返回值應為固定值(final)。DelayQueue內部是使用PriorityQueue實現的。

    DelayQueue = BlockingQueue + PriorityQueue + Delayed

    DelayQueue的關鍵元素BlockingQueue、PriorityQueue、Delayed。可以這么說,DelayQueue是一個使用優先隊列(PriorityQueue)實現的BlockingQueue,優先隊列的比較基準值是時間。

    他們的基本定義如下
    public interface Comparable<T> {
        
    public int compareTo(T o);
    }

    public interface Delayed extends Comparable<Delayed> {
        
    long getDelay(TimeUnit unit);
    }

    public class DelayQueue<extends Delayed> implements BlockingQueue<E> { 
        
    private final PriorityQueue<E> q = new PriorityQueue<E>();
    }

    DelayQueue內部的實現使用了一個優先隊列。當調用DelayQueue的offer方法時,把Delayed對象加入到優先隊列q中。如下:
    public boolean offer(E e) {
        
    final ReentrantLock lock = this.lock;
        lock.lock();
        
    try {
            E first 
    = q.peek();
            q.offer(e);
            
    if (first == null || e.compareTo(first) < 0)
                available.signalAll();
            
    return true;
        } 
    finally {
            lock.unlock();
        }
    }

    DelayQueue的take方法,把優先隊列q的first拿出來(peek),如果沒有達到延時閥值,則進行await處理。如下:
    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) {
                        
    long tl = available.awaitNanos(delay);
                    } 
    else {
                        E x 
    = q.poll();
                        
    assert x != null;
                        
    if (q.size() != 0)
                            available.signalAll(); 
    // wake up other takers
                        return x;

                    }
                }
            }
        } 
    finally {
            lock.unlock();
        }
    }

    -------------------

    以下是Sample,是一個緩存的簡單實現。共包括三個類Pair、DelayItem、Cache。如下:

    public class Pair<K, V> {
        
    public K first;

        
    public V second;
        
        
    public Pair() {}
        
        
    public Pair(K first, V second) {
            
    this.first = first;
            
    this.second = second;
        }
    }

    --------------
    以下是Delayed的實現
    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 object
                return 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;
                
    else
                    
    return 1;
            }
            
    long d = (getDelay(TimeUnit.NANOSECONDS) - other.getDelay(TimeUnit.NANOSECONDS));
            
    return (d == 0? 0 : ((d < 0? -1 : 1);
        }
    }



    以下是Cache的實現,包括了put和get方法,還包括了可執行的main函數。
    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.first, pair.second); 
    // 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)
                q.remove(key);

            
    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 static void main(String[] args) throws Exception {
            Cache
    <Integer, String> cache = new Cache<Integer, String>();
            cache.put(
    1"aaaa"3, TimeUnit.SECONDS);

            Thread.sleep(
    1000 * 2);
            {
                String str 
    = cache.get(1);
                System.out.println(str);
            }

            Thread.sleep(
    1000 * 2);
            {
                String str 
    = cache.get(1);
                System.out.println(str);
            }
        }
    }

    運行Sample,main函數執行的結果是輸出兩行,第一行為aaa,第二行為null。
    posted on 2007-04-27 20:04 溫少的日志 閱讀(2021) 評論(2)  編輯  收藏
    Comments
    • # re: 精巧好用的DelayQueue
      溫少的日志
      Posted @ 2007-04-29 22:52
      原來文章中有DelayItem的,昨天加入一些內容時,不小心把DelayItem部分的代碼刪除了。現已經補上,請看正文。  回復  更多評論   
    • # re: 精巧好用的DelayQueue
      chun
      Posted @ 2012-03-11 14:24
      Cache Demo 有個小小的Bug。設置為 daemon 的 Thread無法停止。
      我加了一個變量 threadRunning = true; while(threadRunning)  回復  更多評論   

    只有注冊用戶登錄后才能發表評論。


    網站導航:
     
     
    主站蜘蛛池模板: 国产精品亚洲а∨天堂2021| 免费在线观看一级毛片| 亚洲最大的黄色网| 国产一区二区三区在线免费| 国产精品免费看久久久香蕉| 亚洲综合一区二区| 免费a级黄色毛片| 国产精品免费AV片在线观看| 亚洲AV无码不卡在线观看下载| 国产一级在线免费观看| 亚洲欧洲久久久精品| 蜜桃成人无码区免费视频网站| 亚洲中文字幕一区精品自拍| 久久亚洲色一区二区三区| 免费观看黄色的网站| 男女交性无遮挡免费视频| 麻豆亚洲av熟女国产一区二| 国产免费黄色大片| 欧洲一级毛片免费| 成全视成人免费观看在线看| 国产午夜亚洲精品国产| 免费特级黄毛片在线成人观看| 日本免费A级毛一片| 亚洲A∨精品一区二区三区下载| 免费v片在线观看品善网| 最近中文字幕免费mv在线视频| 丰满少妇作爱视频免费观看| 中文字幕第一页亚洲| 成人免费男女视频网站慢动作| 亚洲av综合av一区二区三区| 亚洲日本中文字幕| 久久久久亚洲AV成人网| 大陆一级毛片免费视频观看| 一区二区免费视频| 成人无码视频97免费| 怡红院亚洲红怡院在线观看| 亚洲人成人无码网www电影首页| 99re视频精品全部免费| 国产乱子伦精品免费视频| 亚洲国产精品精华液| 亚洲AV色吊丝无码|