溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

linkedblockingqueue如何在java中使用

發(fā)布時(shí)間:2021-04-21 17:18:22 來(lái)源:億速云 閱讀:225 作者:Leah 欄目:編程語(yǔ)言

本篇文章為大家展示了linkedblockingqueue如何在java中使用,內(nèi)容簡(jiǎn)明扼要并且容易理解,絕對(duì)能使你眼前一亮,通過(guò)這篇文章的詳細(xì)介紹希望你能有所收獲。

Java的特點(diǎn)有哪些

Java的特點(diǎn)有哪些 1.Java語(yǔ)言作為靜態(tài)面向?qū)ο缶幊陶Z(yǔ)言的代表,實(shí)現(xiàn)了面向?qū)ο罄碚?,允許程序員以優(yōu)雅的思維方式進(jìn)行復(fù)雜的編程。 2.Java具有簡(jiǎn)單性、面向?qū)ο?、分布式、安全性、平臺(tái)獨(dú)立與可移植性、動(dòng)態(tài)性等特點(diǎn)。 3.使用Java可以編寫桌面應(yīng)用程序、Web應(yīng)用程序、分布式系統(tǒng)和嵌入式系統(tǒng)應(yīng)用程序等。

1.put方法使用事項(xiàng)

(1)使用putLock加鎖;

(2)如果隊(duì)列滿了就阻塞在notFull條件上;

(3)否則就入隊(duì);

(4)如果入隊(duì)后元素?cái)?shù)量小于容量,喚醒其它阻塞在notFull條件上的線程;

(5)釋放鎖;

(6)如果放元素之前隊(duì)列長(zhǎng)度為0,就喚醒notEmpty條件;

2.put增加元素實(shí)例

 public void put(E e) throws InterruptedException {
        if (e == null) throw new NullPointerException();   //e不能為null
        int c = -1;
        Node<E> node = new Node<E>(e);
        final ReentrantLock putLock = this.putLock;     //獲取put鎖
        final AtomicInteger count = this.count;          //獲取count
        putLock.lockInterruptibly();
        try {
            while (count.get() == capacity) {        //如果滿了,那么就需要使用notFull阻塞
                notFull.await();
            }
            enqueue(node);
            c = count.getAndIncrement();
            if (c + 1 < capacity)                    //如果此時(shí)又有空間了,那么notFull喚醒
                notFull.signal();
        } finally {
            putLock.unlock();             //釋放鎖
        }
        if (c == 0)            //當(dāng)c為0時(shí)候,也要根take鎖說(shuō)一下,并發(fā)下
            signalNotEmpty();        //調(diào)用notEmpty        
    }
public E take() throws InterruptedException {
    E x;
    int c = -1;
    final AtomicInteger count = this.count;
    final ReentrantLock takeLock = this.takeLock;
    // 使用takeLock加鎖
    takeLock.lockInterruptibly();
    try {
        // 如果隊(duì)列無(wú)元素,則阻塞在notEmpty條件上
        while (count.get() == 0) {
            notEmpty.await();
        }
        // 否則,出隊(duì)
        x = dequeue();
        // 獲取出隊(duì)前隊(duì)列的長(zhǎng)度
        c = count.getAndDecrement();
        // 如果取之前隊(duì)列長(zhǎng)度大于1,則喚醒notEmpty
        if (c > 1)
            notEmpty.signal();
    } finally {
        // 釋放鎖
        takeLock.unlock();
    }
    // 如果取之前隊(duì)列長(zhǎng)度等于容量
    // 則喚醒notFull
    if (c == capacity)
        signalNotFull();
    return x;
}
 
private E dequeue() {
    // head節(jié)點(diǎn)本身是不存儲(chǔ)任何元素的
    // 這里把head刪除,并把head下一個(gè)節(jié)點(diǎn)作為新的值
    // 并把其值置空,返回原來(lái)的值
    Node<E> h = head;
    Node<E> first = h.next;
    h.next = h; // help GC
    head = first;
    E x = first.item;
    first.item = null;
    return x;
}
 
private void signalNotFull() {
    final ReentrantLock putLock = this.putLock;
    putLock.lock();
    try {
        // 喚醒notFull
        notFull.signal();
    } finally {
        putLock.unlock();
    }
}

上述內(nèi)容就是linkedblockingqueue如何在java中使用,你們學(xué)到知識(shí)或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識(shí)儲(chǔ)備,歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI