Java並發包中的同步隊列SynchronousQueue實現原理
介紹
Java 6的並發編程包中的SynchronousQueue是一個沒有數據緩衝的BlockingQueue,生產者線程對其的插入操作put必須等待消費者的移除操作take,反過來也一樣。
不像ArrayBlockingQueue或LinkedListBlockingQueue,SynchronousQueue內部並沒有數據緩 存空間,你不能調用peek()方法來看隊列中是否有數據元素,因為數據元素隻有當你試著取走的時候才可能存在,不取走而隻想偷窺一下是不行的,當然遍曆 這個隊列的操作也是不允許的。隊列頭元素是第一個排隊要插入數據的線程,而不是要交換的數據。數據是在配對的生產者和消費者線程之間直接傳遞的,並不會將數據緩衝數據到隊列中。可以這樣來理解:生產者和消費者互相等待對方,握手,然後一起離開。
SynchronousQueue的一個使用場景是在線程池裏。Executors.newCachedThreadPool()就使用了 SynchronousQueue,這個線程池根據需要(新任務到來時)創建新的線程,如果有空閑線程則會重複使用,線程空閑了60秒後會被回收。
實現原理
阻塞隊列的實現方法有許多:
阻塞算法實現
阻塞算法實現通常在內部采用一個鎖來保證多個線程中的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(); } }
信號量實現
經典同步隊列實現采用了三個信號量,代碼很簡單,比較容易理解:
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; } }
Java 5的實現相對來說做了一些優化,隻使用了一個鎖,使用隊列代替信號量也可以允許發布者直接發布數據,而不是要首先從阻塞在信號量處被喚醒。
Java6實現
Java 6的SynchronousQueue的實現采用了一種性能更好的無鎖算法 — 擴展的“Dual stack and Dual queue” 算法。性能比Java5的實現有較大提升。競爭機製支持公平和非公平兩種:非公平競爭模式使用的數據結構是後進先出棧(Lifo Stack);公平競爭模式則使用先進先出隊列(Fifo Queue),性能上兩者是相當的,一般情況下,Fifo通常可以支持更大的吞吐量,但Lifo可以更大程度的保持線程的本地化。
代碼實現裏的Dual Queue或Stack內部是用鏈表(LinkedList)來實現的,其節點狀態為以下三種情況:
- 持有數據 – put()方法的元素
- 持有請求 – 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 && nanos <= 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;
}
}
}
文章轉自 並發編程網-ifeve.com
最後更新:2017-05-22 20:04:52