LinkedBlockingQueue

put

public void put(E e) throws InterruptedException {
    if (e == null) throw new NullPointerException();
    // Note: convention in all put/take/etc is to preset local var
    // holding count negative to indicate failure unless set.
    int c = -1;
    Node<E> node = new Node<E>(e);
    final ReentrantLock putLock = this.putLock;
    final AtomicInteger count = this.count;
    // 獲取putLock等缀,可中斷
    putLock.lockInterruptibly();
    try {
        /*
         * Note that count is used in wait guard even though it is
         * not protected by lock. This works because count can
         * only decrease at this point (all other puts are shut
         * out by lock), and we (or some other waiting put) are
         * signalled if it ever changes from capacity. Similarly
         * for all other uses of count in other wait guards.
         */
        // notFull代表鬓长,鏈表未滿
        // 這里如果鏈表已滿荸频,當然該條件不滿足就要等待
        while (count.get() == capacity) {
            notFull.await();
        }
        // 到這里我抠,說明鏈表還有空位铲觉,加到尾部
        enqueue(node);
        c = count.getAndIncrement();
        // 如果當前鏈表還沒滿森缠,那么條件滿足祝闻,所以要喚醒游昼,無需再等待甘苍。
        // 為什么是signal,而不是signalall呢酱床,本來就是一個一個添加的羊赵,全部喚醒只是浪費CPU而已。
        if (c + 1 < capacity)
            notFull.signal();
    } finally {
        // 釋放putLock
        putLock.unlock();
    }
    // notEmpty條件代表扇谣,鏈表非空
    // 如果c==0昧捷,說明鏈表中至少有一個node,那么條件滿足罐寨,所以要喚醒靡挥,無需繼續(xù)等待
    if (c == 0)
        signalNotEmpty();
}

offer

// 跟put類似,唯一不同是這里如果鏈表已滿鸯绿,那么會立即返回false
// 不會一直等到鏈表不滿為止
public boolean offer(E e) {
    if (e == null) throw new NullPointerException();
    final AtomicInteger count = this.count;
    // 當鏈表已滿跋破,那么直接返回false
    if (count.get() == capacity)
        return false;
    int c = -1;
    Node<E> node = new Node<E>(e);
    final ReentrantLock putLock = this.putLock;
    putLock.lock();
    try {
        // 當鏈表未滿簸淀,那么繼續(xù)執(zhí)行入列的操作
        if (count.get() < capacity) {
            enqueue(node);
            c = count.getAndIncrement();
            if (c + 1 < capacity)
                notFull.signal();
        }
    } finally {
        putLock.unlock();
    }
    if (c == 0)
        signalNotEmpty();
    return c >= 0;
}

poll

public E poll() {
    final AtomicInteger count = this.count;
    // 如果鏈表為空,直接返回null
    if (count.get() == 0)
        return null;
    E x = null;
    int c = -1;
    final ReentrantLock takeLock = this.takeLock;
    // 獲取takelock
    takeLock.lock();
    try {
        // 如果鏈表不為空毒返,那么繼續(xù)
        if (count.get() > 0) {
            // head出棧
            x = dequeue();
            c = count.getAndDecrement();
            // 如果當前鏈表不為空租幕,那么notEmpty條件滿足,喚醒
            if (c > 1)
                notEmpty.signal();
        }
    } finally {
        takeLock.unlock();
    }
    // 如果當前鏈表移除一個的情況下拧簸,至少有一個空位劲绪,notfull條件滿足,有必要喚醒
    if (c == capacity)
        signalNotFull();
    return x;
}

take

public E take() throws InterruptedException {
    E x;
    int c = -1;
    final AtomicInteger count = this.count;
    final ReentrantLock takeLock = this.takeLock;
    takeLock.lockInterruptibly();
    try {
        // 跟poll不同的是盆赤,這里如果鏈表為空的話贾富,會一直等待
        while (count.get() == 0) {
            notEmpty.await();
        }
        x = dequeue();
        c = count.getAndDecrement();
        if (c > 1)
            notEmpty.signal();
    } finally {
        takeLock.unlock();
    }
    if (c == capacity)
        signalNotFull();
    return x;
}

peek

public E peek() {
    if (count.get() == 0)
        return null;
    final ReentrantLock takeLock = this.takeLock;
    takeLock.lock();
    try {
        // 返回頭節(jié)點,但并不移除它
        Node<E> first = head.next;
        if (first == null)
            return null;
        else
            return first.item;
    } finally {
        takeLock.unlock();
    }
}

remove

public boolean remove(Object o) {
    if (o == null) return false;
    // 拿鎖牺六,putlock+takelock
    fullyLock();
    try {
        // 從head往后遍歷
        for (Node<E> trail = head, p = trail.next;
             p != null;
             trail = p, p = p.next) {
            // 如果能找到該對象颤枪,移除該節(jié)點
            if (o.equals(p.item)) {
                unlink(p, trail);
                return true;
            }
        }
        return false;
    } finally {
        // 解鎖淑际,takelock+putlock
        fullyUnlock();
    }
}

void fullyLock() {
    putLock.lock();
    takeLock.lock();
}

void fullyUnlock() {
    takeLock.unlock();
    putLock.unlock();
}
void unlink(Node<E> p, Node<E> trail) {
    // assert isFullyLocked();
    // p.next is not changed, to allow iterators that are
    // traversing p to maintain their weak-consistency guarantee.
    p.item = null;
    // 跳過也就是孤立p節(jié)點
    trail.next = p.next;
    // 如果p是尾節(jié)點
    if (last == p)
        // 那么設置trail為尾節(jié)點,也就是移除p節(jié)點
        last = trail;
    // unlink的結(jié)果春缕,當然鏈表未滿霍骄,那么需要喚醒notFull條件
    if (count.getAndDecrement() == capacity)
        notFull.signal();
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市簿训,隨后出現(xiàn)的幾起案子咱娶,更是在濱河造成了極大的恐慌强品,老刑警劉巖,帶你破解...
    沈念sama閱讀 217,542評論 6 504
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件的榛,死亡現(xiàn)場離奇詭異琼了,居然都是意外死亡,警方通過查閱死者的電腦和手機夫晌,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,822評論 3 394
  • 文/潘曉璐 我一進店門雕薪,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人晓淀,你說我怎么就攤上這事所袁。” “怎么了凶掰?”我有些...
    開封第一講書人閱讀 163,912評論 0 354
  • 文/不壞的土叔 我叫張陵燥爷,是天一觀的道長蜈亩。 經(jīng)常有香客問我,道長前翎,這世上最難降的妖魔是什么稚配? 我笑而不...
    開封第一講書人閱讀 58,449評論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮港华,結(jié)果婚禮上道川,老公的妹妹穿的比我還像新娘。我一直安慰自己苹丸,他們只是感情好愤惰,可當我...
    茶點故事閱讀 67,500評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著赘理,像睡著了一般宦言。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上商模,一...
    開封第一講書人閱讀 51,370評論 1 302
  • 那天奠旺,我揣著相機與錄音,去河邊找鬼施流。 笑死响疚,一個胖子當著我的面吹牛,可吹牛的內(nèi)容都是我干的瞪醋。 我是一名探鬼主播忿晕,決...
    沈念sama閱讀 40,193評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼银受!你這毒婦竟也來了践盼?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,074評論 0 276
  • 序言:老撾萬榮一對情侶失蹤宾巍,失蹤者是張志新(化名)和其女友劉穎咕幻,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體顶霞,經(jīng)...
    沈念sama閱讀 45,505評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡肄程,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,722評論 3 335
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了选浑。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片蓝厌。...
    茶點故事閱讀 39,841評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖鲜侥,靈堂內(nèi)的尸體忽然破棺而出褂始,到底是詐尸還是另有隱情,我是刑警寧澤描函,帶...
    沈念sama閱讀 35,569評論 5 345
  • 正文 年R本政府宣布狐粱,位于F島的核電站胆数,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏蒋搜。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,168評論 3 328
  • 文/蒙蒙 一豆挽、第九天 我趴在偏房一處隱蔽的房頂上張望券盅。 院中可真熱鬧,春花似錦锰镀、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,783評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至刨肃,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間之景,已是汗流浹背锻狗。 一陣腳步聲響...
    開封第一講書人閱讀 32,918評論 1 269
  • 我被黑心中介騙來泰國打工焕参, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人叠纷。 一個月前我還...
    沈念sama閱讀 47,962評論 2 370
  • 正文 我出身青樓,卻偏偏與公主長得像崇众,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子顷歌,可洞房花燭夜當晚...
    茶點故事閱讀 44,781評論 2 354

推薦閱讀更多精彩內(nèi)容