亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Java并發(fā)編程之ConcurrentLinkedQueue解讀

 更新時間:2023年12月26日 10:58:06   作者:緣來如此09  
這篇文章主要介紹了Java并發(fā)編程之ConcurrentLinkedQueue解讀,非阻塞的實(shí)現(xiàn)方式則可以使用循環(huán)CAS的方式來實(shí)現(xiàn),而ConcurrentLinkedQueue就是juc包中自帶的經(jīng)典非堵塞方式實(shí)現(xiàn)的工具類,需要的朋友可以參考下

一、簡介

工作中有時候需要使用線程安全的隊(duì)列。如果要實(shí)現(xiàn)一個線程安全的隊(duì)列有兩種方式:一種是使用阻塞算法,另一種是使用非阻塞算法。使用阻塞算法的隊(duì)列可以用一個鎖(入隊(duì)和出隊(duì)用同一把鎖)或兩個鎖(入隊(duì)和出隊(duì)用不同的鎖)等方式來實(shí)現(xiàn)。非阻塞的實(shí)現(xiàn)方式則可以使用循環(huán)CAS的方式來實(shí)現(xiàn)。而ConcurrentLinkedQueue就是juc包中自帶的經(jīng)典非堵塞方式實(shí)現(xiàn)的工具類

二、結(jié)構(gòu)

ConcurrentLinkedQueue由head節(jié)點(diǎn)和tail節(jié)點(diǎn)組成,每個節(jié)點(diǎn)(Node)由節(jié)點(diǎn)元素(item)和指向下一個節(jié)點(diǎn)(next)的引用組成,節(jié)點(diǎn)與節(jié)點(diǎn)之間就是通過這個next關(guān)聯(lián)起來,從而組成一張鏈表結(jié)構(gòu)的隊(duì)列。默認(rèn)情況下head節(jié)點(diǎn)存儲的元素為空,tail節(jié)點(diǎn)等于head節(jié)點(diǎn)。

private transient volatile Node<E> tail = head;

三、入隊(duì)

從源代碼角度來看,整個入隊(duì)過程主要做兩件事情:第一是定位出尾節(jié)點(diǎn);第二是使用CAS算法將入隊(duì)節(jié)點(diǎn)設(shè)置成尾節(jié)點(diǎn)的next節(jié)點(diǎn),如不成功則重試。

    public boolean offer(E e) {
        checkNotNull(e);
        // 入隊(duì)前,創(chuàng)建一個入隊(duì)節(jié)點(diǎn)
        final Node<E> newNode = new Node<E>(e);
        for (Node<E> t = tail, p = t;;) {
            // 創(chuàng)建一個指向tail節(jié)點(diǎn)的引用
            Node<E> q = p.next;
            if (q == null) {
                // p is last node
                if (p.casNext(null, newNode)) {
                    // Successful CAS is the linearization point
                    // for e to become an element of this queue,
                    // and for newNode to become "live".
                    if (p != t) // hop two nodes at a time
                        casTail(t, newNode);  // Failure is OK.
                    return true;
                }
                // Lost CAS race to another thread; re-read next
            }
            else if (p == q)
                // We have fallen off list.  If tail is unchanged, it
                // will also be off-list, in which case we need to
                // jump to head, from which all live nodes are always
                // reachable.  Else the new tail is a better bet.
                p = (t != (t = tail)) ? t : head;
            else
                // Check for tail updates after two hops.
                p = (p != t && t != (t = tail)) ? t : q;
        }
    }

tail節(jié)點(diǎn)并不總是尾節(jié)點(diǎn),所以每次入隊(duì)都必須先通過tail節(jié)點(diǎn)來找到尾節(jié)點(diǎn)。尾節(jié)點(diǎn)可能是tail節(jié)點(diǎn),也可能是tail節(jié)點(diǎn)的next節(jié)點(diǎn)。代碼中循環(huán)體中的第一個if就是判斷tail是否有next節(jié)點(diǎn),有則表示next節(jié)點(diǎn)可能是尾節(jié)點(diǎn)。獲取tail節(jié)點(diǎn)的next節(jié)點(diǎn)需要注意的是p節(jié)點(diǎn)等于p的next節(jié)點(diǎn)的情況,只有一種可能就是p節(jié)點(diǎn)和p的next節(jié)點(diǎn)都等于空,表示這個隊(duì)列剛初始化,正準(zhǔn)備添加節(jié)點(diǎn),所以需要返回head節(jié)點(diǎn)。

/**
*返回 p 的后繼節(jié)點(diǎn),或者如果 p.next 已經(jīng)鏈接到 self 則返回頭節(jié)點(diǎn),這只有在使用現(xiàn)在不在列表*中的陳舊指針遍歷時才會為真。 
**/
   final Node<E> succ(Node<E> p) {
        Node<E> next = p.next;
        return (p == next) ? head : next;
    }

四、出列

public E poll() {
        restartFromHead:
        for (;;) {
            for (Node<E> h = head, p = h, q;;) {
                //入列折騰的tail,那出列折騰的就是head
                E item = p.item;
                //出列判斷依據(jù)是節(jié)點(diǎn)的item=null
                //item != null, 并且能將操作節(jié)點(diǎn)的item設(shè)置null, 表示出列成功
                if (item != null && p.casItem(item, null)) {
                    if (p != h) 
                        //一旦出列成功需要對head進(jìn)行移動
                        updateHead(h, ((q = p.next) != null) ? q : p);
                    return item;
                }
                else if ((q = p.next) == null) {
                    updateHead(h, p);
                    return null;
                }
                else if (p == q)
                    //第一輪操作失敗,下一輪繼續(xù),調(diào)回到循環(huán)前
                    continue restartFromHead;
                else
                    //推動head節(jié)點(diǎn)移動
                    p = q;
            }
        }
    }

五、ConcurrentLinkedQueue使用特點(diǎn)

ConcurrentLinkedQueue使用約定:

1:不允許null入列

2:在入隊(duì)的最后一個元素的next為null

3:隊(duì)列中所有未刪除的節(jié)點(diǎn)的item都不能為null且都能從head節(jié)點(diǎn)遍歷到

4:刪除節(jié)點(diǎn)是將item設(shè)置為null, 隊(duì)列迭代時跳過item為null節(jié)點(diǎn)

5:head節(jié)點(diǎn)跟tail不一定指向頭節(jié)點(diǎn)或尾節(jié)點(diǎn),可能存在滯后性

到此這篇關(guān)于Java并發(fā)編程之ConcurrentLinkedQueue解讀的文章就介紹到這了,更多相關(guān)Java中的ConcurrentLinkedQueue內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論