In ArrayBlockingQueue, why copy final member field into local final variable?

Posted by mjlee on Stack Overflow See other posts from Stack Overflow or by mjlee
Published on 2010-05-07T03:15:13Z Indexed on 2010/05/07 3:58 UTC
Read the original article Hit count: 155

Filed under:
|

In ArrayBlockingQueue, any method that requires lock will get set 'final' local 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 set a local variable 'lock' from 'this.lock' when field 'this.lock' is final also.

Additionally, it also set local variable of E[] before acting on.

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 to local final variable?

© Stack Overflow or respective owner

Related posts about java

Related posts about optimization