在ArrayBlockingQueue中,為什么要將最終成員字段復(fù)制到局部變量中?在……里面ArrayBlockingQueue,所有需要鎖的方法都將其復(fù)制到本地。final變量,然后調(diào)用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();
}}有什么理由復(fù)制this.lock到局部變量lock當(dāng)田野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;}是否有任何理由將最終字段復(fù)制到局部變量中?
添加回答
舉報
0/150
提交
取消