亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Java中常用阻塞隊(duì)列的問(wèn)題小結(jié)

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

Java常用阻塞隊(duì)列

ArrayBlockingQueue

內(nèi)部由一個(gè)固定長(zhǎng)度的數(shù)組來(lái)實(shí)現(xiàn)阻塞隊(duì)列

/** 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();
    /** 定長(zhǎng)數(shù)組 */
    this.items = new Object[capacity]; 
    lock = new ReentrantLock(fair);
    notEmpty = lock.newCondition();
    notFull =  lock.newCondition();
}

提供了兩個(gè)入隊(duì)操作方法,offer()和put()
offer方法不會(huì)阻塞,但有返回值,如果隊(duì)列滿(mǎn)了,那么直接返回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()會(huì)在隊(duì)列滿(mǎn)了的時(shí)候會(huì)阻塞生產(chǎn)者線(xiàn)程,知道有消費(fèi)者線(xiàn)程消費(fèi)后將其喚醒。

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(); // 出隊(duì)后喚醒生產(chǎn)者線(xiàn)程
    return x;
}

LinkedBlockingQueue

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

需要注意的是,如果構(gòu)造一個(gè)LinkedBlockingQueue對(duì)象,而沒(méi)有指定其容量大小,LinkedBlockingQueue會(huì)默認(rèn)一個(gè)類(lèi)似無(wú)限大小的容量(Integer.MAX_VALUE),這樣的話(huà),如果生產(chǎn)者的速度一旦大于消費(fèi)者的速度,也許還沒(méi)有等到隊(duì)列滿(mǎ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 實(shí)現(xiàn)生產(chǎn)者消費(fèi)者問(wè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常用阻塞隊(duì)列的文章就介紹到這了,更多相關(guān)Java阻塞隊(duì)列內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • emoji表情與unicode編碼互轉(zhuǎn)的實(shí)現(xiàn)(JS,JAVA,C#)

    emoji表情與unicode編碼互轉(zhuǎn)的實(shí)現(xiàn)(JS,JAVA,C#)

    這篇文章主要介紹了emoji表情與unicode編碼互轉(zhuǎn)的實(shí)現(xiàn)(JS,JAVA,C#),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-01-01
  • Java使用HttpUtils實(shí)現(xiàn)發(fā)送HTTP請(qǐng)求

    Java使用HttpUtils實(shí)現(xiàn)發(fā)送HTTP請(qǐng)求

    這篇文章主要介紹了Java使用HttpUtils實(shí)現(xiàn)發(fā)送HTTP請(qǐng)求,HTTP請(qǐng)求,在日常開(kāi)發(fā)中,還是比較常見(jiàn)的,今天給大家分享HttpUtils如何使用,需要的朋友可以參考下
    2023-05-05
  • Mybatis-Plus的使用詳解

    Mybatis-Plus的使用詳解

    這篇文章主要介紹了Mybatis-Plus的使用詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-10-10
  • Java讀取數(shù)據(jù)庫(kù)表的示例代碼

    Java讀取數(shù)據(jù)庫(kù)表的示例代碼

    這篇文章主要介紹了Java讀取數(shù)據(jù)庫(kù)表,本文通過(guò)示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-05-05
  • Java Web用戶(hù)登錄實(shí)例代碼

    Java Web用戶(hù)登錄實(shí)例代碼

    這篇文章主要介紹了Java Web用戶(hù)登錄實(shí)例代碼的相關(guān)資料,非常不錯(cuò)具有參考借鑒價(jià)值,感興趣的朋友一起看看吧
    2016-05-05
  • 解決IDEA 左側(cè)Project中沒(méi)有out文件夾的問(wèn)題

    解決IDEA 左側(cè)Project中沒(méi)有out文件夾的問(wèn)題

    這篇文章主要介紹了解決IDEA 左側(cè)Project中沒(méi)有out文件夾的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2021-02-02
  • Spring聲明式事務(wù)配置使用詳解

    Spring聲明式事務(wù)配置使用詳解

    這篇文章主要介紹了在spring注解中,使用聲明式事務(wù),需要用到兩個(gè)核心的注解:@Transactional注解和@EnableTransactionManagement注解。將@Transactional注解加在方法上,@EnableTransactionManagement注解加在配置類(lèi)上
    2022-08-08
  • SpringBoot同一接口多個(gè)實(shí)現(xiàn)類(lèi)配置的實(shí)例詳解

    SpringBoot同一接口多個(gè)實(shí)現(xiàn)類(lèi)配置的實(shí)例詳解

    這篇文章主要介紹了SpringBoot同一接口多個(gè)實(shí)現(xiàn)類(lèi)配置的實(shí)例詳解,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-11-11
  • 詳解Eclipse安裝SVN插件的兩種方法

    詳解Eclipse安裝SVN插件的兩種方法

    這篇文章主要介紹了詳解Eclipse 安裝 SVN 插件的兩種方法,詳細(xì)的介紹了這兩種安裝方法,具有一定的參考價(jià)值,有興趣的可以了解一下
    2018-01-01
  • 深入java內(nèi)存查看與分析詳解

    深入java內(nèi)存查看與分析詳解

    本篇文章是對(duì)java內(nèi)存查看進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
    2013-05-05

最新評(píng)論