其中ArrayBlockingQueue
,所有需要锁的方法final
在调用之前将其复制到一个局部变量中lock()
。
public boolean offer(E e) {
if (e == null) throw new NullPointerException();
final ReentrantLock lock = this.lock;
lock.lock();
try {
if (count == items.length)
return false;
else {
insert(e);
return true;
}
} finally {
lock.unlock();
}
}
当字段为时,是否有任何理由复制this.lock
到局部变量?lock
this.lock
final
此外,它还在E[]
操作之前使用了本地副本:
private E extract() {
final E[] items = this.items;
E x = items[takeIndex];
items[takeIndex] = null;
takeIndex = inc(takeIndex);
--count;
notFull.signal();
return x;
}
是否有任何理由将最终字段复制到本地最终变量?