A problem about Java BlockingQueue source code learning

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?


There is the same question on

stackoverflow .

.copying to locals produces the smallest bytecode, and for low-level code it's nice to write code that's a little closer to the machine

it's too low-level. Ignore it


copy in to reduce the cost of "getting fields".
it's a squeeze performance

.
Menu