可能重复:
In ArrayBlockingQueue, why copy final member field into local final variable?
从ThreadPoolExecutor中查看此代码:
private final ReentrantLock mainLock = new ReentrantLock();
...
private void ensureQueuedTaskHandled(Runnable command) {
final ReentrantLock mainLock = this.mainLock;
mainLock.lock();
为什么this.mainLock
被复制到局部变量?
我知道这个模式对于确保字段不会改变很有用,但我们在这里讨论的是最后一个字段。它仍然相关吗?