2016-09-15 4 views
2

ArrayBlockingQueue 구현에서 전역 변수가 직접 액세스되지 않는 이유는 무엇입니까? poll 방법에 ArrayBlockingQueue : 전역 변수에 직접 액세스하지 않는 이유는 무엇입니까?

public class ArrayBlockingQueue<E> extends AbstractQueue<E> 
      implements BlockingQueue<E>, java.io.Serializable { 

    /** The queued items */ 
    final Object[] items; 

    /** Main lock guarding all access */ 
    final ReentrantLock lock; 

    // ... 

    public ArrayBlockingQueue(int capacity, boolean fair) { 
     if (capacity <= 0) 
      throw new IllegalArgumentException(); 
     this.items = new Object[capacity]; 
     lock = new ReentrantLock(fair); 
     // ... 
    } 

    @Override 
    public E poll() { 
     final ReentrantLock lock = this.lock; // Why the global variable assigned to a local variable ? 
     lock.lock(); 
     try { 
      return (count == 0) ? null : extract(); 
     } finally { 
      lock.unlock(); 
     } 
    } 

} 

는 글로벌 ReentrantLock와 lock 변수는 로컬 변수에 할당하고 참조 사용이다. 왜 그렇게?

답변

0

로컬 변수는 스택에 저장되고 더 빠르게 액세스됩니다.

+0

이것은 사이트에서 찾고있는 답변이 아니며,이 말은 가장 좋고 가난한 의견입니다. –