In ArrayBlockingQueue
, all the methods that require the lock copy it to a local final
variable before calling 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();
}
}
Is there any reason to copy this.lock
to a local variable lock
when the field this.lock
is final
?
Additionally, it also uses a local copy of E[]
before acting on it:
private E extract() {
final E[] items = this.items;
E x = items[takeIndex];
items[takeIndex] = null;
takeIndex = inc(takeIndex);
--count;
notFull.signal();
return x;
}
Is there any reason for copying a final field to a local final variable?