阅读java.util.concurrent.ArrayBlockingQueue代码时,看到在其put方法中第3、4行中,特意定义局部常量赋予this.items和this.lock的值。请问各位这样做有什么好处???
public void put(E e) throws InterruptedException {
        if (e == null) throw new NullPointerException();
        final E[] items = this.items;
        final ReentrantLock lock = this.lock;
        lock.lockInterruptibly();
        try {
            try {
                while (count == items.length)
                    notFull.await();
            } catch (InterruptedException ie) {
                notFull.signal(); // propagate to non-interrupted thread
                throw ie;
            }
            insert(e);
        } finally {
            lock.unlock();
        }
    }

解决方案 »

  1.   

    避免混淆,this指代的是该对象的filed或方法等,避免和局部变量,形参等混淆。
      

  2.   

    感觉无特别优势,估计之前是没有,或是由于某原因 定那两引用指向其他对象,后来再指回来,之后干脆就保留这两个引用,又和this之下的同名,确实有点微妙...
      

  3.   

    他还特意把items 和lock 这两个局部变量定义为final
      

  4.   

    关键是final,得到后值就不能改变了