一種簡單無鎖隊列的實現

來源:互聯網
上載者:User

Disruptor是記憶體無鎖並發架構,基於一個環數組作為緩衝,詳見Disruptor-1.0。

下面是自己設計的一個簡易版,目前沒有發現存在衝突或錯誤的測試案例。大家可以一起測試下。

package tianshui.lockfree.queue;import java.io.Serializable;import java.lang.reflect.Array;import java.util.concurrent.atomic.AtomicInteger;/** * 並發生產者和消費者數不能多於queue的length,預設是2^16 * @author 天水 * @date 2013-4-9 上午11:42:10 */public class RingBuffer<T> implements Serializable {/** *  */private static final long serialVersionUID = 6976960108708949038L;private volatile AtomicInteger head;private volatile AtomicInteger tail;private int length;final T EMPTY = null;private volatile T[] queue;public RingBuffer(Class<T> type, int length){this.head = new AtomicInteger(0);this.tail = new AtomicInteger(0);this.length = length == 0 ? 2 << 16 : length; // 預設2^16this.queue = (T[]) Array.newInstance(type, this.length);  }public void enQueue(T t){if(t == null) t= (T) new Object();// 阻塞 -- 避免多產生者迴圈生產同一個節點while(this.getTail() - this.getHead() >= this.length); int ctail = this.tail.getAndIncrement();while(this.queue[this.getTail(ctail)] != EMPTY); // 自旋this.queue[this.getTail(ctail)] = t;}public T deQueue(){T t = null;// 阻塞 -- 避免多消費者迴圈消費同一個節點while(this.head.get() >= this.tail.get()); int chead = this.head.getAndIncrement();while(this.queue[this.getHead(chead)] == EMPTY); // 自旋t = this.queue[this.getHead(chead)];this.queue[this.getHead(chead)] = EMPTY;return t;}public int getHead(int index){return index & (this.length - 1);}public int getTail(int index) {return index & (this.length - 1);}public int getHead() {return head.get() & (this.length - 1);}public int getTail() {return tail.get() & (this.length - 1);}public T[] getQueue() {return queue;}public int getLength() {return length;}public void setLength(int length) {this.length = length;}}

下面是測試代碼:

package tianshui.lockfree.queue;import java.util.concurrent.atomic.AtomicInteger;/** * @author 天水 * @date 2013-4-9 下午04:13:29 */public class TestBuffer {public static AtomicInteger index = new AtomicInteger(0);public static void main(String[] args){int tCount = 10; // thread countint length = 0;  // buffer length -> 2^16final RingBuffer<Integer> buffer = new RingBuffer<Integer>(Integer.class, length);// providerRunnable pr = new Runnable(){@Overridepublic void run() {while(true){try {Thread.sleep(100);} catch (InterruptedException e) {e.printStackTrace();}int tindex = index.getAndIncrement();buffer.enQueue(tindex);System.out.println("buffer enQueue: " + tindex);try {Thread.sleep(100);} catch (InterruptedException e) {e.printStackTrace();}}}};// consumerRunnable cr = new Runnable(){@Overridepublic void run() {while(true){try {Thread.sleep(100);} catch (InterruptedException e) {e.printStackTrace();}Integer cindex = buffer.deQueue();System.out.println("buffer deQueue: " + cindex);try {Thread.sleep(100);} catch (InterruptedException e) {e.printStackTrace();}}}};for(int i=0; i<tCount; i++){new Thread(cr).start();}for(int i=0; i<tCount; i++){new Thread(pr).start();}}}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.