Java源碼解析阻塞隊(duì)列ArrayBlockingQueue常用方法
本文基于jdk1.8進(jìn)行分析
ArrayBlockingQueue的功能簡(jiǎn)介參考http://chabaoo.cn/article/154211.htm。
首先看一下ArrayBlockingQueue的成員變量。如下圖。最主要的成員變量是items,它是一個(gè)Object類型的數(shù)組用于保存阻塞隊(duì)列中的元素。其次是takeIndex,putIndex,count,分別表示了從隊(duì)列獲取元素的位置,往隊(duì)列里放元素的位置和隊(duì)列中元素的個(gè)數(shù)。然后是lock,notEmpty和notFull三個(gè)和鎖相關(guān)的成員變量。lock是一個(gè)可重入鎖,而notEmpty和notFull是和lock綁定的2個(gè)Condition。對(duì)可重入鎖不是很了解的同學(xué),可以參考這篇文章http://chabaoo.cn/article/154207.htm。對(duì)可重入鎖的理解,是理解ArrayBlockingQueue的基礎(chǔ)。也可以這么說(shuō),理解了可重入鎖,那么在理解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;
接下來(lái)介紹ArrayBlockingQueue的主要方法。首先是入隊(duì)方法。ArrayBlockingQueue的入隊(duì)方法有好幾個(gè),功能略有差異,下面我們逐一介紹各個(gè)入隊(duì)方法。首先看一下put方法,如下圖。put方法的功能是,往隊(duì)列尾部插入指定元素,如果隊(duì)列已滿,那么就等待可用空間。方法的實(shí)現(xiàn)過(guò)程是,首先判斷元素是否非空。然后,進(jìn)行加鎖,加鎖后判斷隊(duì)列是否已滿。如果已滿,則等待不滿條件。當(dāng)被喚醒后,進(jìn)行入隊(duì)操作。入隊(duì)方法中,會(huì)喚醒在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(); }
另一個(gè)入隊(duì)方法是offer,代碼如下。這個(gè)方法與add方法的區(qū)別是,offer方法是立刻返回的,它并不像add方法那樣,當(dāng)隊(duì)列滿時(shí)會(huì)一直等待。
/** * 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(); } }
接下來(lái)看一下出隊(duì)方法take,代碼如下。首先對(duì)可重入鎖加鎖,然后判斷元素個(gè)數(shù)是否為0.如果為0,則等待不空條件,否則進(jìn)行出隊(duì)操作。
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é)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請(qǐng)查看下面相關(guān)鏈接
- java ArrayBlockingQueue阻塞隊(duì)列的實(shí)現(xiàn)示例
- Java中ArrayBlockingQueue和LinkedBlockingQueue
- Java 并發(fā)編程ArrayBlockingQueue的實(shí)現(xiàn)
- java ArrayBlockingQueue的方法及缺點(diǎn)分析
- Java源碼解析阻塞隊(duì)列ArrayBlockingQueue介紹
- Java源碼解析阻塞隊(duì)列ArrayBlockingQueue功能簡(jiǎn)介
- 詳細(xì)分析Java并發(fā)集合ArrayBlockingQueue的用法
- java并發(fā)之ArrayBlockingQueue詳細(xì)介紹
- Java并發(fā)編程ArrayBlockingQueue的使用
相關(guān)文章
Spring的RedisTemplate存儲(chǔ)的key和value有特殊字符的處理
這篇文章主要介紹了Spring的RedisTemplate存儲(chǔ)的key和value有特殊字符的處理方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-12-12idea中創(chuàng)建多module的maven工程的方法
這篇文章主要介紹了idea中創(chuàng)建多module的maven工程的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-10-10Java實(shí)現(xiàn)獲取行政區(qū)劃的示例代碼
這篇文章主要為大家詳細(xì)介紹了如何利用Java語(yǔ)言實(shí)現(xiàn)獲取行政區(qū)劃的功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)游戲2023-03-03Java微信二次開(kāi)發(fā)(二) Java微信文本消息接口請(qǐng)求與發(fā)送
這篇文章主要為大家詳細(xì)介紹了Java微信二次開(kāi)發(fā)第二篇,Java微信文本消息接口請(qǐng)求與發(fā)送功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-04-04Jackson處理Optional時(shí)遇到問(wèn)題的解決與分析
Optional是Java實(shí)現(xiàn)函數(shù)式編程的強(qiáng)勁一步,并且?guī)椭诜妒街袑?shí)現(xiàn),但是Optional的意義顯然不止于此,下面這篇文章主要給大家介紹了關(guān)于Jackson處理Optional時(shí)遇到問(wèn)題的解決與分析的相關(guān)資料,需要的朋友可以參考下2022-02-02詳解jeefast和Mybatis實(shí)現(xiàn)二級(jí)聯(lián)動(dòng)的問(wèn)題
這篇文章主要介紹了詳解jeefast和Mybatis實(shí)現(xiàn)二級(jí)聯(lián)動(dòng)的問(wèn)題,本文通過(guò)圖文實(shí)例相結(jié)合給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-10-10Java實(shí)現(xiàn)獲取Excel中的表單控件
Excel中可通過(guò)【開(kāi)發(fā)工具】菜單欄下插入表單控件,如文本框、單選按鈕、復(fù)選框、組合框等等。本文將利用Java實(shí)現(xiàn)獲取Excel中的表單控件,需要的可以參考一下2022-05-05Java對(duì)象深復(fù)制與淺復(fù)制實(shí)例詳解
這篇文章主要介紹了 Java對(duì)象深復(fù)制與淺復(fù)制實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2017-05-05