成人午夜视频全免费观看高清-秋霞福利视频一区二区三区-国产精品久久久久电影小说-亚洲不卡区三一区三区一区

java中SynchronousQueue的公平和非公平同步隊列

本篇內容主要講解“java中SynchronousQueue的公平和非公平同步隊列”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“java中SynchronousQueue的公平和非公平同步隊列”吧!

創(chuàng)新互聯(lián)公司長期為上1000+客戶提供的網站建設服務,團隊從業(yè)經驗10年,關注不同地域、不同群體,并針對不同對象提供差異化的產品和服務;打造開放共贏平臺,與合作伙伴共同營造健康的互聯(lián)網生態(tài)環(huán)境。為茅箭企業(yè)提供專業(yè)的成都網站制作、做網站、外貿營銷網站建設,茅箭網站改版等技術服務。擁有10多年豐富建站經驗和眾多成功案例,為您定制開發(fā)。

SynchronousQueue 分為公平和非公平同步隊列
構造方法中傳入true,表示公平隊列這時選擇通過TransferQueue中的方法實現(xiàn)出入元素FIFO
否則傳入false,使用TransferStack實現(xiàn)出入元素,沒有固定順序

TransfererStack中的節(jié)點組成
static final class SNode{
    volatile SNode next;
    volatile SNode match;
    volatile Thread waiter;
    Object item;
    int mode;

    volatile SNode head;
}


 static {
                try {
                    UNSAFE = sun.misc.Unsafe.getUnsafe();
                    Class<?> k = SNode.class;
                    matchOffset = UNSAFE.objectFieldOffset
                        (k.getDeclaredField("match"));
                    nextOffset = UNSAFE.objectFieldOffset
                        (k.getDeclaredField("next"));
                } catch (Exception e) {
                    throw new Error(e);
                }
            }

對 transfer方法的實現(xiàn)
E transfer(E e, boolean timed, long nanos) {
            /*
             * Basic algorithm is to loop trying one of three actions:
             *
             * 1. If apparently empty or already containing nodes of same
             *    mode, try to push node on stack and wait for a match,
             *    returning it, or null if cancelled.
             *
             * 2. If apparently containing node of complementary mode,
             *    try to push a fulfilling node on to stack, match
             *    with corresponding waiting node, pop both from
             *    stack, and return matched item. The matching or
             *    unlinking might not actually be necessary because of
             *    other threads performing action 3:
             *
             * 3. If top of stack already holds another fulfilling node,
             *    help it out by doing its match and/or pop
             *    operations, and then continue. The code for helping
             *    is essentially the same as for fulfilling, except
             *    that it doesn't return the item.
             */

            SNode s = null; // constructed/reused as needed
            int mode = (e == null) ? REQUEST : DATA;

            for (;;) {
                SNode h = head;
                if (h == null || h.mode == mode) {  // empty or same-mode
                    if (timed && nanos <= 0) {      // can't wait
                        if (h != null && h.isCancelled())
                            casHead(h, h.next);     // pop cancelled node
                        else
                            return null;
                    } else if (casHead(h, s = snode(s, e, h, mode))) {
                        SNode m = awaitFulfill(s, timed, nanos);
                        if (m == s) {               // wait was cancelled
                            clean(s);
                            return null;
                        }
                        if ((h = head) != null && h.next == s)
                            casHead(h, s.next);     // help s's fulfiller
                        return (E) ((mode == REQUEST) ? m.item : s.item);
                    }
                } else if (!isFulfilling(h.mode)) { // try to fulfill
                    if (h.isCancelled())            // already cancelled
                        casHead(h, h.next);         // pop and retry
                    else if (casHead(h, s=snode(s, e, h, FULFILLING|mode))) {
                        for (;;) { // loop until matched or waiters disappear
                            SNode m = s.next;       // m is s's match
                            if (m == null) {        // all waiters are gone
                                casHead(s, null);   // pop fulfill node
                                s = null;           // use new node next time
                                break;              // restart main loop
                            }
                            SNode mn = m.next;
                            if (m.tryMatch(s)) {
                                casHead(s, mn);     // pop both s and m
                                return (E) ((mode == REQUEST) ? m.item : s.item);
                            } else                  // lost match
                                s.casNext(m, mn);   // help unlink
                        }
                    }
                } else {                            // help a fulfiller
                    SNode m = h.next;               // m is h's match
                    if (m == null)                  // waiter is gone
                        casHead(h, null);           // pop fulfilling node
                    else {
                        SNode mn = m.next;
                        if (m.tryMatch(h))          // help match
                            casHead(h, mn);         // pop both h and m
                        else                        // lost match
                            h.casNext(m, mn);       // help unlink
                    }
                }
            }
        }


    TransferQueue中的節(jié)點組成

    static final class QNode{
        volatile QNode next;
        volatile Object item;
        volatile Thread waiter;
        final boolean isData;
        transient volatile QNode head;
        transient volatile QNode tail;

    }

到此,相信大家對“java中SynchronousQueue的公平和非公平同步隊列”有了更深的了解,不妨來實際操作一番吧!這里是創(chuàng)新互聯(lián)網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續(xù)學習!

當前標題:java中SynchronousQueue的公平和非公平同步隊列
URL標題:http://jinyejixie.com/article20/jojoco.html

成都網站建設公司_創(chuàng)新互聯(lián),為您提供品牌網站建設、網站建設、虛擬主機、域名注冊關鍵詞優(yōu)化、網站營銷

廣告

聲明:本網站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)

小程序開發(fā)
丹寨县| 河东区| 昭苏县| 苍溪县| 天津市| 洛川县| 盐城市| 日喀则市| 古浪县| 惠东县| 雷州市| 绥芬河市| 安康市| 桃园市| 兴城市| 秦皇岛市| 武宁县| 凤台县| 长子县| 左贡县| 巴中市| 四会市| 富裕县| 石屏县| 增城市| 北宁市| 江口县| 随州市| 勃利县| 广东省| 互助| 化州市| 兴隆县| 万盛区| 册亨县| 灵台县| 佛学| 阳谷县| 綦江县| 鹤庆县| 合川市|