为何ArrayBlockingQueue中的ReentranLock对象无需实例化?

看了ArrayBlockingQueue的源码

public class ArrayBlockingQueue<E> extends AbstractQueue<E>        implements BlockingQueue<E>, java.io.Serializable {
 
    final Object[] items;  
    int takeIndex;  
    int putIndex;   
    int count;    
    final ReentrantLock lock;    /**notEmpty条件对象,用于通知take方法队列已有元素,可执行获取操作 */
    private final Condition notEmpty;    
    private final Condition notFull;   
       迭代器
     */
    transient Itrs itrs = null;

    public void put(E e) throws InterruptedException {
     checkNotNull(e);      
     final ReentrantLock lock = this.lock;  // ????????
      lock.lockInterruptibly();
      try {          
          while (count == items.length)              //将当前调用线程挂起,添加到notFull条件队列中等待唤醒
              notFull.await();
          enqueue(e);//如果队列没有满直接添加。。
      } finally {
          lock.unlock();
      }
  }
}

为甚么类的属性里没有ReentrantLock lock = new ReentrantLock();

却可以在put()中直接指定ReentrantLock lock=this.lock;?

阿Dine
浏览 1155回答 3
3回答

慕雪5558698

好好看看源码吧你

谁动了我的萝卜

初始化的时候调用
打开App,查看更多内容
随时随地看视频慕课网APP