ArrayBlockingQueue
public E take() throws InterruptedException {
final ReentrantLock lock = this.lock;
lock.lockInterruptibly();
try {
while (count == 0)
notEmpty.await();
return dequeue();
} finally {
lock.unlock();
}
}
为什么要方法内先通过声明final类型的变量来获取对this.lock的引用?直接用不可以吗?
赋值给局部变量,接下来就不需要考虑这个变量的并发修改问题了。成员变量存在并发中,没有办法保证它的状态不会在其他线程中被修改。