first attach the BlockingQueue source code take () code:
public class ArrayBlockingQueue<E> implements BlockingQueue<E> {
final ReentrantLock lock;
//lock
public ArrayBlockingQueue<E>(){
//...
}
public E take() throws InterruptedException {
final ReentrantLock lock = this.lock; //
lock.lockInterruptibly();
try {
while (count == 0)
notEmpty.await();
return dequeue();
} finally {
lock.unlock();
}
}
}
Why does the first line final ReentrantLock lock = this.lock, in the take method copy the global field lock to a local variable first? Is it not possible to use global final lock directly (eg.) This.lock.lockInterruptibly ())?? Why bother?