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

これはサイトが探している回答ではありませんが、これは最高のコメントであり、貧しいものです。 –

関連する問題