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

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

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


    我們談一下實(shí)際的場(chǎng)景吧。我們?cè)陂_發(fā)中,有如下場(chǎng)景

    a) 關(guān)閉空閑連接。服務(wù)器中,有很多客戶端的連接,空閑一段時(shí)間之后需要關(guān)閉之。
    b) 緩存。緩存中的對(duì)象,超過了空閑時(shí)間,需要從緩存中移出。
    c) 任務(wù)超時(shí)處理。在網(wǎng)絡(luò)協(xié)議滑動(dòng)窗口請(qǐng)求應(yīng)答式交互時(shí),處理超時(shí)未響應(yīng)的請(qǐng)求。

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

    這場(chǎng)景,使用DelayQueue最適合了。

    DelayQueue 是java.util.concurrent中提供的一個(gè)很有意思的類。很巧妙,非常棒!但是java doc和Java SE 5.0的source中都沒有提供Sample。我最初在閱讀ScheduledThreadPoolExecutor源碼時(shí),發(fā)現(xiàn)DelayQueue 的妙用。隨后在實(shí)際工作中,應(yīng)用在session超時(shí)管理,網(wǎng)絡(luò)應(yīng)答通訊協(xié)議的請(qǐng)求超時(shí)處理。

    本文將會(huì)對(duì)DelayQueue做一個(gè)介紹,然后列舉應(yīng)用場(chǎng)景。并且提供一個(gè)Delayed接口的實(shí)現(xiàn)和Sample代碼。

    DelayQueue是一個(gè)BlockingQueue,其特化的參數(shù)是Delayed。(不了解BlockingQueue的同學(xué),先去了解BlockingQueue再看本文)
    Delayed擴(kuò)展了Comparable接口,比較的基準(zhǔn)為延時(shí)的時(shí)間值,Delayed接口的實(shí)現(xiàn)類getDelay的返回值應(yīng)為固定值(final)。DelayQueue內(nèi)部是使用PriorityQueue實(shí)現(xiàn)的。

    DelayQueue = BlockingQueue + PriorityQueue + Delayed

    DelayQueue的關(guān)鍵元素BlockingQueue、PriorityQueue、Delayed??梢赃@么說,DelayQueue是一個(gè)使用優(yōu)先隊(duì)列(PriorityQueue)實(shí)現(xiàn)的BlockingQueue,優(yōu)先隊(duì)列的比較基準(zhǔn)值是時(shí)間。

    他們的基本定義如下
    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內(nèi)部的實(shí)現(xiàn)使用了一個(gè)優(yōu)先隊(duì)列。當(dāng)調(diào)用DelayQueue的offer方法時(shí),把Delayed對(duì)象加入到優(yōu)先隊(duì)列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方法,把優(yōu)先隊(duì)列q的first拿出來(peek),如果沒有達(dá)到延時(shí)閥值,則進(jìn)行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,是一個(gè)緩存的簡(jiǎn)單實(shí)現(xiàn)。共包括三個(gè)類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的實(shí)現(xiàn)
    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的實(shí)現(xiàn),包括了put和get方法,還包括了可執(zhí)行的main函數(shù)。
    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) {
                        
    // 超時(shí)對(duì)象處理
                        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.");
        }

        
    // 添加緩存對(duì)象
        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);
        }

        
    // 測(cè)試入口函數(shù)
        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);
            }
        }
    }

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

    只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


    網(wǎng)站導(dǎo)航:
     
     
    主站蜘蛛池模板: 黄色免费网址大全| 国产精品色拉拉免费看| 四虎影院永久免费观看| 久久成人a毛片免费观看网站| 中文字幕乱码亚洲精品一区| 国产一级高青免费| 亚洲AV无码乱码精品国产| 黄色免费网站在线看| 国产人成免费视频| 亚洲成a人片在线不卡一二三区| 成人毛片免费播放| 亚洲AV男人的天堂在线观看| 免费被黄网站在观看| 亚洲heyzo专区无码综合| 俄罗斯极品美女毛片免费播放 | 91人成网站色www免费下载| 亚洲精品天堂成人片?V在线播放| 国产精品亚洲综合一区在线观看| 破了亲妺妺的处免费视频国产| 久久亚洲AV成人无码国产电影| 四虎影在线永久免费四虎地址8848aa| 麻豆va在线精品免费播放| 亚洲国产精品自在拍在线播放 | 亚洲乱码一二三四区麻豆| 无码乱肉视频免费大全合集| 亚洲综合久久精品无码色欲| 免费在线观看一级毛片| 在线观看人成视频免费无遮挡| 伊人久久综在合线亚洲2019| 女人被弄到高潮的免费视频| 无码免费又爽又高潮喷水的视频 | 久久精品国产亚洲AV电影| 久久久久国产精品免费免费搜索| 日韩色日韩视频亚洲网站| 综合久久久久久中文字幕亚洲国产国产综合一区首| eeuss影院免费92242部| 亚洲手机中文字幕| 免费看国产一级片| 亚洲精品卡2卡3卡4卡5卡区| 亚洲国产精品久久久久久| 亚洲AV成人一区二区三区AV|