为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

阿Dine 2018-09-08 19:18:43
看了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;?
查看完整描述

3 回答

已采纳
?
慕雪5558698

TA贡献3条经验 获得超4个赞

好好看看源码吧你

查看完整回答
反对 回复 2018-09-09
  • 阿Dine
    阿Dine
    老哥 我看了好久 还是看不懂 请教一下您 我很想知道答案
  • 阿Dine
    阿Dine
    哦!!! 我看到了!! 原来在构造器里啊!!!!
?
谁动了我的萝卜

TA贡献6条经验 获得超1个赞

初始化的时候调用https://img1.sycdn.imooc.com//5ca56486000196e213080448.jpg

查看完整回答
反对 回复 2019-04-04
  • 3 回答
  • 0 关注
  • 1144 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信