Java併發包中的同步隊列SynchronousQueue實現原理

介紹

Java 6的併發編程包中的是SynchronousQueue一個沒有數據緩衝的BlockingQueue,生產者線程對其的插入操做put必須等待消費者的移除操做take,反過來也同樣。html

不像ArrayBlockingQueue或LinkedListBlockingQueue,SynchronousQueue內部並無數據緩存空間,你不能調用peek()方法來看隊列中是否有數據元素,由於數據元素只有當你試着取走的時候纔可能存在,不取走而只想偷窺一下是不行的,固然遍歷這個隊列的操做也是不容許的。隊列頭元素是第一個排隊要插入數據的線程,而不是要交換的數據。數據是在配對的生產者和消費者線程之間直接傳遞的,並不會將數據緩衝數據到隊列中。能夠這樣來理解:生產者和消費者互相等待對方,握手,而後一塊兒離開。java

SynchronousQueue的一個使用場景是在線程池裏。Executors.newCachedThreadPool()就使用了SynchronousQueue,這個線程池根據須要(新任務到來時)建立新的線程,若是有空閒線程則會重複使用,線程空閒了60秒後會被回收。node

實現原理

阻塞隊列的實現方法有許多:算法

阻塞算法實現

阻塞算法實現一般在內部採用一個鎖來保證多個線程中的put()和take()方法是串行執行的。採用鎖的開銷是比較大的,還會存在一種狀況是線程A持有線程B須要的鎖,B必須一直等待A釋放鎖,即便A可能一段時間內由於B的優先級比較高而得不到時間片運行。因此在高性能的應用中咱們經常但願規避鎖的使用。編程

public class NativeSynchronousQueue<E> {
    boolean putting = false;
    E item = null;

    public synchronized E take() throws InterruptedException {
        while (item == null)
            wait();
        E e = item;
        item = null;
        notifyAll();
        return e;
    }

    public synchronized void put(E e) throws InterruptedException {
        if (e==null) return;
        while (putting)
            wait();
        putting = true;
        item = e;
        notifyAll();
        while (item!=null)
            wait();
        putting = false;
        notifyAll();
    }
}

信號量實現

經典同步隊列實現採用了三個信號量,代碼很簡單,比較容易理解:api

public class SemaphoreSynchronousQueue<E> {
    E item = null;
    Semaphore sync = new Semaphore(0);
    Semaphore send = new Semaphore(1);
    Semaphore recv = new Semaphore(0);

    public E take() throws InterruptedException {
        recv.acquire();
        E x = item;
        sync.release();
        send.release();
        return x;
    }

    public void put (E x) throws InterruptedException{
        send.acquire();
        item = x;
        recv.release();
        sync.acquire();
    }
}

在多核機器上,上面方法的同步代價仍然較高,操做系統調度器須要上千個時間片來阻塞或喚醒線程,而上面的實現即便在生產者put()時已經有一個消費者在等待的狀況下,阻塞和喚醒的調用仍然須要。緩存

Java 5實現

public class Java5SynchronousQueue<E> {
    ReentrantLock qlock = new ReentrantLock();
    Queue waitingProducers = new Queue();
    Queue waitingConsumers = new Queue();

    static class Node extends AbstractQueuedSynchronizer {
        E item;
        Node next;

        Node(Object x) { item = x; }
        void waitForTake() { /* (uses AQS) */ }
           E waitForPut() { /* (uses AQS) */ }
    }

    public E take() {
        Node node;
        boolean mustWait;
        qlock.lock();
        node = waitingProducers.pop();
        if(mustWait = (node == null))
           node = waitingConsumers.push(null);
         qlock.unlock();

        if (mustWait)
           return node.waitForPut();
        else
            return node.item;
    }

    public void put(E e) {
         Node node;
         boolean mustWait;
         qlock.lock();
         node = waitingConsumers.pop();
         if (mustWait = (node == null))
             node = waitingProducers.push(e);
         qlock.unlock();

         if (mustWait)
             node.waitForTake();
         else
            node.item = e;
    }
}

 

ava 5的實現相對來講作了一些優化,只使用了一個鎖,使用隊列代替信號量也能夠容許發佈者直接發佈數據,而不是要首先從阻塞在信號量處被喚醒。數據結構

Java6實現

Java 6的SynchronousQueue的實現採用了一種性能更好的無鎖算法 — 擴展的「Dual stack and Dual queue」算法。性能比Java5的實現有較大提高。競爭機制支持公平和非公平兩種:非公平競爭模式使用的數據結構是後進先出棧(Lifo Stack);公平競爭模式則使用先進先出隊列(Fifo Queue),性能上二者是至關的,通常狀況下,Fifo一般能夠支持更大的吞吐量,但Lifo能夠更大程度的保持線程的本地化。併發

代碼實現裏的Dual Queue或Stack內部是用鏈表(LinkedList)來實現的,其節點狀態爲如下三種狀況:oracle

  1. 持有數據 – put()方法的元素
  2. 持有請求 – take()方法

這個算法的特色就是任何操做均可以根據節點的狀態判斷執行,而不須要用到鎖。

其核心接口是Transfer,生產者的put或消費者的take都使用這個接口,根據第一個參數來區別是入列(棧)仍是出列(棧)。

/**
    * Shared internal API for dual stacks and queues.
    */
   static abstract class Transferer {
       /**
        * Performs a put or take.
        *
        * @param e if non-null, the item to be handed to a consumer;
        *          if null, requests that transfer return an item
        *          offered by producer.
        * @param timed if this operation should timeout
        * @param nanos the timeout, in nanoseconds
        * @return if non-null, the item provided or received; if null,
        *         the operation failed due to timeout or interrupt --
        *         the caller can distinguish which of these occurred
        *         by checking Thread.interrupted.
        */
       abstract Object transfer(Object e, boolean timed, long nanos);
   }

TransferQueue實現以下(摘自Java 6源代碼),入列和出列都基於Spin和CAS方法:

/**
    * Puts or takes an item.
    */
   Object transfer(Object e, boolean timed, long nanos) {
       /* Basic algorithm is to loop trying to take either of
        * two actions:
        *
        * 1. If queue apparently empty or holding same-mode nodes,
        *    try to add node to queue of waiters, wait to be
        *    fulfilled (or cancelled) and return matching item.
        *
        * 2. If queue apparently contains waiting items, and this
        *    call is of complementary mode, try to fulfill by CAS'ing
        *    item field of waiting node and dequeuing it, and then
        *    returning matching item.
        *
        * In each case, along the way, check for and try to help
        * advance head and tail on behalf of other stalled/slow
        * threads.
        *
        * The loop starts off with a null check guarding against
        * seeing uninitialized head or tail values. This never
        * happens in current SynchronousQueue, but could if
        * callers held non-volatile/final ref to the
        * transferer. The check is here anyway because it places
        * null checks at top of loop, which is usually faster
        * than having them implicitly interspersed.
        */

       QNode s = null; // constructed/reused as needed
       boolean isData = (e != null);

       for (;;) {
           QNode t = tail;
           QNode h = head;
           if (t == null || h == null)         // saw uninitialized value
               continue;                       // spin

           if (h == t || t.isData == isData) { // empty or same-mode
               QNode tn = t.next;
               if (t != tail)                  // inconsistent read
                   continue;
               if (tn != null) {               // lagging tail
                   advanceTail(t, tn);
                   continue;
               }
               if (timed &amp;&amp; nanos &lt;= 0)        // can't wait
                   return null;
               if (s == null)
                   s = new QNode(e, isData);
               if (!t.casNext(null, s))        // failed to link in
                   continue;

               advanceTail(t, s);              // swing tail and wait
               Object x = awaitFulfill(s, e, timed, nanos);
               if (x == s) {                   // wait was cancelled
                   clean(t, s);
                   return null;
               }

               if (!s.isOffList()) {           // not already unlinked
                   advanceHead(t, s);          // unlink if head
                   if (x != null)              // and forget fields
                       s.item = s;
                   s.waiter = null;
               }
               return (x != null)? x : e;

           } else {                            // complementary-mode
               QNode m = h.next;               // node to fulfill
               if (t != tail || m == null || h != head)
                   continue;                   // inconsistent read

               Object x = m.item;
               if (isData == (x != null) ||    // m already fulfilled
                   x == m ||                   // m cancelled
                   !m.casItem(x, e)) {         // lost CAS
                   advanceHead(h, m);          // dequeue and retry
                   continue;
               }

               advanceHead(h, m);              // successfully fulfilled
               LockSupport.unpark(m.waiter);
               return (x != null)? x : e;
           }
       }
   }

參考文章

  1. Javadoc of SynchronousQueue
  2. Scalable Synchronous Queues
  3. Nonblocking Concurrent Data Structures with Condition Synchronization
相關文章
相關標籤/搜索