欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java源碼解析阻塞隊列ArrayBlockingQueue常用方法

 更新時間:2019年01月08日 09:49:09   作者:李燦輝  
今天小編就為大家分享一篇關(guān)于Java源碼解析阻塞隊列ArrayBlockingQueue常用方法,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧

本文基于jdk1.8進行分析

ArrayBlockingQueue的功能簡介參考http://www.dbjr.com.cn/article/154211.htm

首先看一下ArrayBlockingQueue的成員變量。如下圖。最主要的成員變量是items,它是一個Object類型的數(shù)組用于保存阻塞隊列中的元素。其次是takeIndex,putIndex,count,分別表示了從隊列獲取元素的位置,往隊列里放元素的位置和隊列中元素的個數(shù)。然后是lock,notEmpty和notFull三個和鎖相關(guān)的成員變量。lock是一個可重入鎖,而notEmpty和notFull是和lock綁定的2個Condition。對可重入鎖不是很了解的同學(xué),可以參考這篇文章http://www.dbjr.com.cn/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)過程是,首先判斷元素是否非空。然后,進行加鎖,加鎖后判斷隊列是否已滿。如果已滿,則等待不滿條件。當被喚醒后,進行入隊操作。入隊方法中,會喚醒在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方法那樣,當隊列滿時會一直等待。

  /**
   * 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)鏈接

相關(guān)文章

  • java計算時間差的方法

    java計算時間差的方法

    這篇文章主要介紹了java計算時間差的方法,涉及java針對時間的轉(zhuǎn)換與計算相關(guān)技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-07-07
  • 用SpringMVC編寫一個HelloWorld的詳細過程

    用SpringMVC編寫一個HelloWorld的詳細過程

    SpringMVC是Spring的一個后續(xù)產(chǎn)品,是Spring的一個子項目<BR>SpringMVC?是?Spring?為表述層開發(fā)提供的一整套完備的解決方案,本文我們將用SpringMVC編寫一個HelloWorld,文中有詳細的編寫過程,需要的朋友可以參考下
    2023-08-08
  • Retrofit+RxJava實現(xiàn)帶進度條的文件下載

    Retrofit+RxJava實現(xiàn)帶進度條的文件下載

    這篇文章主要為大家詳細介紹了Retrofit+RxJava實現(xiàn)帶進度條的文件下載,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-06-06
  • Java8中的類型注解淺析

    Java8中的類型注解淺析

    這篇文章主要介紹了Java8中的類型注解淺析,java8之前,注解只能是在聲明的地方所使用,java8里面的注解則可以應(yīng)用在任何地方,需要的朋友可以參考下
    2014-06-06
  • spring boot項目中MongoDB的使用方法

    spring boot項目中MongoDB的使用方法

    前段時間分享了關(guān)于Spring Boot中使用Redis的文章,除了Redis之后,我們在互聯(lián)網(wǎng)產(chǎn)品中還經(jīng)常會用到另外一款著名的NoSQL數(shù)據(jù)庫MongoDB。下面這篇文章主要給大家介紹了關(guān)于在spring boot項目中MongoDB的使用方法,需要的朋友可以參考下。
    2017-09-09
  • java 在Jetty9中使用HttpSessionListener和Filter

    java 在Jetty9中使用HttpSessionListener和Filter

    這篇文章主要介紹了java 在Jetty9中使用HttpSessionListener和Filter的相關(guān)資料,需要的朋友可以參考下
    2017-06-06
  • Java 二叉樹遍歷的常用方法

    Java 二叉樹遍歷的常用方法

    二叉樹的遍歷可以說是解決二叉樹問題的基礎(chǔ)。我們常用的遍歷方式無外乎就四種 前序遍歷、中序遍歷、后續(xù)遍歷、層次遍歷 這四種。
    2021-05-05
  • springboot?無法自動裝配的問題

    springboot?無法自動裝配的問題

    這篇文章主要介紹了springboot?無法自動裝配的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-01-01
  • 詳解java裝飾模式(Decorator Pattern)

    詳解java裝飾模式(Decorator Pattern)

    這篇文章主要為大家詳細介紹了java裝飾模式Decorator Pattern,這種類型的設(shè)計模式屬于結(jié)構(gòu)型模式,它是作為現(xiàn)有的類的一個包裝,對裝飾器模式感興趣的小伙伴們可以參考一下
    2016-04-04
  • springboot實現(xiàn)對注解的切面案例

    springboot實現(xiàn)對注解的切面案例

    這篇文章主要介紹了springboot實現(xiàn)對注解的切面過程,首先定義一個注解、再編寫對注解的切面只是記錄的執(zhí)行時間和打印方法,可以實現(xiàn)其他邏輯,需要的朋友可以參考一下
    2022-01-01

最新評論