In many Java sources (e.g. LinkedBlockingDeque), I see things like this:
final ReentrantLock lock = new ReentrantLock();
public void putLast(E e) throws InterruptedException {
final ReentrantLock lock = this.lock;
lock.lock();
try {
} finally {
lock.unlock();
}
}
I understand the basic pattern (lock, unlock eventually), but my question is: why assign a local lock variable before using it? Why is this instead of the next?
final ReentrantLock lock = new ReentrantLock();
public void putLast(E e) throws InterruptedException {
this.lock.lock();
try {
} finally {
lock.unlock();
}
}
Will it affect optimization? Can the first example prevent lock escalation?
EDIT after comments . Please do not add an answer if you really do not know why this is so. This is from a Java source, the @author tag is Doug Lea, so I'm sure it is there for some reason. Do not indicate that the code is simply equivalent.
thank