最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java中常用阻塞隊列的問題小結(jié)

 更新時間:2022年01月26日 11:42:35   作者:碼出地球  
這篇文章主要介紹了Java常用阻塞隊列問題,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下

Java常用阻塞隊列

ArrayBlockingQueue

內(nèi)部由一個固定長度的數(shù)組來實現(xiàn)阻塞隊列

/** 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;

public ArrayBlockingQueue(int capacity, boolean fair) {
    if (capacity <= 0)
        throw new IllegalArgumentException();
    /** 定長數(shù)組 */
    this.items = new Object[capacity]; 
    lock = new ReentrantLock(fair);
    notEmpty = lock.newCondition();
    notFull =  lock.newCondition();
}

提供了兩個入隊操作方法,offer()和put()
offer方法不會阻塞,但有返回值,如果隊列滿了,那么直接返回false,否則插入數(shù)據(jù)并返回true。

/**
 * 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();
    }
}

put()會在隊列滿了的時候會阻塞生產(chǎn)者線程,知道有消費者線程消費后將其喚醒。

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();
    }
}

private E dequeue() {
    // assert lock.getHoldCount() == 1;
    // assert items[takeIndex] != null;
    final Object[] items = this.items;
    @SuppressWarnings("unchecked")
    E x = (E) items[takeIndex];
    items[takeIndex] = null;
    if (++takeIndex == items.length)
        takeIndex = 0;
    count--;
    if (itrs != null)
        itrs.elementDequeued();
    notFull.signal(); // 出隊后喚醒生產(chǎn)者線程
    return x;
}

LinkedBlockingQueue

基于鏈表的阻塞隊列,同ArrayListBlockingQueue類似,其內(nèi)部也維持著一個數(shù)據(jù)緩沖隊列(該隊列由一個鏈表構(gòu)成),當(dāng)生產(chǎn)者往隊列中放入一個數(shù)據(jù)時,隊列會從生產(chǎn)者手中獲取數(shù)據(jù),并緩存在隊列內(nèi)部,而生產(chǎn)者立即返回;只有當(dāng)隊列緩沖區(qū)達到最大值緩存容量時,才會阻塞生產(chǎn)者隊列,直到消費者從隊列中消費掉一份數(shù)據(jù),生產(chǎn)者線程會被喚醒,反之對于消費者這端的處理也基于同樣的原理。

需要注意的是,如果構(gòu)造一個LinkedBlockingQueue對象,而沒有指定其容量大小,LinkedBlockingQueue會默認(rèn)一個類似無限大小的容量(Integer.MAX_VALUE),這樣的話,如果生產(chǎn)者的速度一旦大于消費者的速度,也許還沒有等到隊列滿阻塞產(chǎn)生,系統(tǒng)內(nèi)存就有可能已被消耗殆盡了。

/**
 * Creates a {@code LinkedBlockingQueue} with a capacity of
 * {@link Integer#MAX_VALUE}.
 */
public LinkedBlockingQueue() {
    this(Integer.MAX_VALUE);
}

/**
 * Creates a {@code LinkedBlockingQueue} with the given (fixed) capacity.
 *
 * @param capacity the capacity of this queue
 * @throws IllegalArgumentException if {@code capacity} is not greater
 *         than zero
 */
public LinkedBlockingQueue(int capacity) {
    if (capacity <= 0) throw new IllegalArgumentException();
    this.capacity = capacity;
    last = head = new Node<E>(null);
}

使用 BlockingQueue 實現(xiàn)生產(chǎn)者消費者問題

public class ProducerConsumer {

    private static BlockingQueue<String> queue = new ArrayBlockingQueue<>(5);
    private static class Producer extends Thread {
        @Override
        public void run() {
            try {
                queue.put("product");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.print("produce..");
        }
    }
    private static class Consumer extends Thread {
                String product = queue.take();
            System.out.print("consume..");
}
public static void main(String[] args) {
    for (int i = 0; i < 2; i++) {
        Producer producer = new Producer();
        producer.start();
    for (int i = 0; i < 5; i++) {
        Consumer consumer = new Consumer();
        consumer.start();
    for (int i = 0; i < 3; i++) {
output:
produce..produce..consume..consume..produce..consume..produce..consume..produce..consume..

到此這篇關(guān)于Java常用阻塞隊列的文章就介紹到這了,更多相關(guān)Java阻塞隊列內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 詳解JNA中的回調(diào)方法

    詳解JNA中的回調(diào)方法

    這篇文章主要介紹了JNA中的回調(diào)方法,主要包括JNA 中的 Callback,callback 的應(yīng)用,本文通過實例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2022-05-05
  • 詳解java基礎(chǔ)--提示對話框的使用

    詳解java基礎(chǔ)--提示對話框的使用

    這篇文章主要介紹了java基礎(chǔ)--提示對話框的使用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-03-03
  • 精通Java List 按字段提取對象

    精通Java List 按字段提取對象

    這篇文章主要介紹了精通Java List 按字段提取對象的相關(guān)資料,需要的朋友可以參考下
    2023-11-11
  • java 對象的序列化和反序列化詳細(xì)介紹

    java 對象的序列化和反序列化詳細(xì)介紹

    這篇文章主要介紹了java 對象的序列化和反序列化的相關(guān)資料,需要的朋友可以參考下
    2016-10-10
  • Java項目實現(xiàn)五子棋小游戲

    Java項目實現(xiàn)五子棋小游戲

    這篇文章主要為大家詳細(xì)介紹了Java項目實現(xiàn)五子棋小游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-05-05
  • Java中ArrayList和SubList的坑面試題

    Java中ArrayList和SubList的坑面試題

    集合是Java開發(fā)日常開發(fā)中經(jīng)常會使用到的,下面這篇文章主要給大家介紹了關(guān)于Java中ArrayList和SubList的坑面試題,需要的朋友可以參考下
    2022-05-05
  • Android studio按鈕點擊頁面跳轉(zhuǎn)詳細(xì)步驟

    Android studio按鈕點擊頁面跳轉(zhuǎn)詳細(xì)步驟

    在Android應(yīng)用程序中,頁面跳轉(zhuǎn)是非常常見的操作,下面這篇文章主要給大家介紹了關(guān)于Android studio按鈕點擊頁面跳轉(zhuǎn)的相關(guān)資料,文中通過實例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2023-06-06
  • Java Annotation注解相關(guān)原理代碼總結(jié)

    Java Annotation注解相關(guān)原理代碼總結(jié)

    這篇文章主要介紹了Java Annotation注解相關(guān)原理代碼總結(jié),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-07-07
  • Spring Cloud Alibaba Nacos Config配置中心實現(xiàn)

    Spring Cloud Alibaba Nacos Config配置中心實現(xiàn)

    這篇文章主要介紹了Spring Cloud Alibaba Nacos Config配置中心實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-04-04
  • springboot使用攔截器判斷是否登錄

    springboot使用攔截器判斷是否登錄

    這篇文章主要介紹了springboot使用攔截器判斷是否登錄,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-11-11

最新評論

凤凰县| 嵊州市| 灵寿县| 横山县| 保康县| 全南县| 东兴市| 新晃| 景德镇市| 元氏县| 鄄城县| 汪清县| 泉州市| 天长市| 威海市| 什邡市| 邵阳县| 宁津县| 江油市| 莲花县| 铜山县| 湖北省| 满城县| 平原县| 东乌珠穆沁旗| 阳城县| 城口县| 鄂尔多斯市| 奉贤区| 高台县| 辰溪县| 崇信县| 连城县| 大安市| 都匀市| 崇礼县| 金坛市| 东台市| 孙吴县| 瑞昌市| 淄博市|