日期:2014-05-20  浏览次数:20811 次

请教个方法内常量的问题
阅读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();
  }
  }

------解决方案--------------------
避免混淆,this指代的是该对象的filed或方法等,避免和局部变量,形参等混淆。
------解决方案--------------------
感觉无特别优势,估计之前是没有,或是由于某原因 定那两引用指向其他对象,后来再指回来,之后干脆就保留这两个引用,又和this之下的同名,确实有点微妙...
------解决方案--------------------
关键是final,得到后值就不能改变了。。。
------解决方案--------------------
我的意思是编程时的更改,不是运行时的更改哦,我都有点那种习惯