Java多線程 -- JUC包源碼分析10 -- ConcurrentLinkedQueue源碼分析__Java並發編程

來源:互聯網
上載者:User

在前面的篇章中,我們詳細分析了AQS,並提到了裡面一個關鍵資料結構:所有阻塞線程組成的一個等待隊列,這個隊列是用單向無鎖鏈表實現的。

今天所講的ConcurrentLinkedQueue,其實現和AQS中的無鎖隊列基本一樣。所以,如果你深刻理解了AQS,ConcurrentLinkedQueue也就知道了。出於內容的完整性,在此還是列一下其源碼:

public class ConcurrentLinkedQueue<E> extends AbstractQueue<E>        implements Queue<E>, java.io.Serializable {    //單向鏈表的Node    private static class Node<E> {        private volatile E item;        private volatile Node<E> next;```        。。。    }    //整個隊列記錄1頭1尾2個結點    private transient volatile Node<E> head = new Node<E>(null);    private transient volatile Node<E> tail = head;    //入隊,也即cas tail (樂觀鎖)    public boolean offer(E e) {        if (e == null) throw new NullPointerException();        Node<E> n = new Node<E>(e);        retry:        for (;;) {            Node<E> t = tail;            Node<E> p = t;            for (int hops = 0; ; hops++) {                Node<E> next = succ(p);                if (next != null) {                    if (hops > HOPS && t != tail)                        continue retry;                    p = next;                } else if (p.casNext(null, n)) {                    if (hops >= HOPS)                        casTail(t, n); // Failure is OK.                    return true;                   } else {                    p = succ(p);                }            }        }    }    //出隊,cas head,樂觀鎖    public E poll() {        Node<E> h = head;        Node<E> p = h;        for (int hops = 0; ; hops++) {            E item = p.getItem();            if (item != null && p.casItem(item, null)) {                if (hops >= HOPS) {                    Node<E> q = p.getNext();                    updateHead(h, (q != null) ? q : p);                }                return item;            }            Node<E> next = succ(p);            if (next == null) {                updateHead(h, p);                break;            }            p = next;        }        return null;    }   。。。}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.