日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > java >内容正文

java

阻塞队列 java 源码_Java源码解析阻塞队列ArrayBlockingQueue常用方法

發(fā)布時間:2025/4/5 java 15 豆豆
生活随笔 收集整理的這篇文章主要介紹了 阻塞队列 java 源码_Java源码解析阻塞队列ArrayBlockingQueue常用方法 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

本文基于jdk1.8進行分析

首先看一下ArrayBlockingQueue的成員變量。如下圖。最主要的成員變量是items,它是一個Object類型的數(shù)組用于保存阻塞隊列中的元素。其次是takeIndex,putIndex,count,分別表示了從隊列獲取元素的位置,往隊列里放元素的位置和隊列中元素的個數(shù)。然后是lock,notEmpty和notFull三個和鎖相關(guān)的成員變量。lock是一個可重入鎖,而notEmpty和notFull是和lock綁定的2個Condition。對可重入鎖不是很了解的同學(xué),可以參考這篇文章https://www.jb51.net/article/154207.htm。對可重入鎖的理解,是理解ArrayBlockingQueue的基礎(chǔ)。也可以這么說,理解了可重入鎖,那么在理解ArrayBlockingQueue就很順利了。

/** The queued items **/

final Object[] items;

/** items index for next take, poll, peek or remove **/

int takeIndex;

/** items index for next put, offer, or add **/

int putIndex;

/** Number of elements in the queue **/

int count;

/**

* Concurrency control uses the classic two-condition algorithm

* found in any textbook.

**/

/** Main lock guarding all access **/

final ReentrantLock lock;

/** Condition for waiting takes **/

private final Condition notEmpty;

/** Condition for waiting puts **/

private final Condition notFull;

/**

* Shared state for currently active iterators, or null if there

* are known not to be any. Allows queue operations to update

* iterator state.

**/

transient Itrs itrs = null;

接下來介紹ArrayBlockingQueue的主要方法。首先是入隊方法。ArrayBlockingQueue的入隊方法有好幾個,功能略有差異,下面我們逐一介紹各個入隊方法。首先看一下put方法,如下圖。put方法的功能是,往隊列尾部插入指定元素,如果隊列已滿,那么就等待可用空間。方法的實現(xiàn)過程是,首先判斷元素是否非空。然后,進行加鎖,加鎖后判斷隊列是否已滿。如果已滿,則等待不滿條件。當(dāng)被喚醒后,進行入隊操作。入隊方法中,會喚醒在notEmpty條件上等待的線程。

/**

* Inserts the specified element at the tail of this queue, waiting

* for space to become available if the queue is full.

* @throws InterruptedException {@inheritDoc}

* @throws NullPointerException {@inheritDoc}

**/

public void put(E e) throws InterruptedException {

checkNotNull(e);

final ReentrantLock lock = this.lock;

lock.lockInterruptibly();

try {

while (count == items.length)

notFull.await();

enqueue(e);

} finally {

lock.unlock();

}

}

/**

* Inserts element at current put position, advances, and signals.

* Call only when holding lock.

**/

private void enqueue(E x) {

// assert lock.getHoldCount() == 1;

// assert items[putIndex] == null;

final Object[] items = this.items;

items[putIndex] = x;

if (++putIndex == items.length)

putIndex = 0;

count++;

notEmpty.signal();

}

另一個入隊方法是offer,代碼如下。這個方法與add方法的區(qū)別是,offer方法是立刻返回的,它并不像add方法那樣,當(dāng)隊列滿時會一直等待。

/**

* Inserts the specified element at the tail of this queue if it is

* possible to do so immediately without exceeding the queue's capacity,

* returning {@code true} upon success and {@code false} if this queue

* is full. This method is generally preferable to method {@link #add},

* which can fail to insert an element only by throwing an exception.

* @throws NullPointerException if the specified element is null

**/

public boolean offer(E e) {

checkNotNull(e);

final ReentrantLock lock = this.lock;

lock.lock();

try {

if (count == items.length)

return false;

else {

enqueue(e);

return true;

}

} finally {

lock.unlock();

}

}

接下來看一下出隊方法take,代碼如下。首先對可重入鎖加鎖,然后判斷元素個數(shù)是否為0.如果為0,則等待不空條件,否則進行出隊操作。

public E take() throws InterruptedException {

final ReentrantLock lock = this.lock;

lock.lockInterruptibly();

try {

while (count == 0)

notEmpty.await();

return dequeue();

} finally {

lock.unlock();

}

}

ArrayListBlockingqueue中還有其他相關(guān)方法,這里就不一一介紹了。

總結(jié)

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,謝謝大家對腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請查看下面相關(guān)鏈接

總結(jié)

以上是生活随笔為你收集整理的阻塞队列 java 源码_Java源码解析阻塞队列ArrayBlockingQueue常用方法的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。