문제

In java JRE I saw the code

private final ReentrantLock lock;
public E poll() {
        final ReentrantLock lock = this.lock;
        lock.lock();

Why is lock captured to a private variable? I would expect simply

public E poll() {
        lock.lock();
도움이 되었습니까?

해결책

Primarily it is to ensure maximum performance. Although this is a real micro-optimisation, it's probably in performance-sensitive code, and you might as well make it.

You also need to be very careful that the lock reference you are using doesn't mutate. Sure make the field final, but taking a final local reference is locally explicit.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top