Possible Duplicate:
In ArrayBlockingQueue, why copy final member field into local final variable?
Looking at the implementation of java.util.concurrent.DelayQueue I see a pattern which I don't quite understand.
public class DelayQueue<E extends Delayed> extends AbstractQueue<E>
implements BlockingQueue<E> {
private transient final ReentrantLock lock = new ReentrantLock();
private transient final Condition available = lock.newCondition();
private final PriorityQueue<E> q = new PriorityQueue<E>();
public boolean offer(E e) {
**final ReentrantLock lock = this.lock;**//(*)
lock.lock();
try { ... }
finally {lock.unlock();}
}
public E poll() {
**final ReentrantLock lock = this.lock**;//(*)
lock.lock();
try { ... }
finally {lock.unlock();}
}}
What's the purpose of the (*) line (to work with temp reference to the field) which can be found in implementations of methods from the BlockingQueue interface where some concurrency (locks) is involved? Consider, that in the case the lock field is final and assigned during field intialization with other final fields.