为什么JDK代码这样写?final ReentrantLock takeLock = this.takeLock

  • 栈变量比堆变量是容易被缓存

https://stackoverflow.com/questions/13155860/why-does-jdk-sourcecode-take-a-final-copy-of-volatile-instances/13155900#13155900

This is an idiom typical for lock-free code involving volatile variables. At the first line you read the volatile once and then work with it. In the meantime another thread can update the volatile, but you are only interested in the value you initially read.
这是一个有关volatile变量的lock-free的典型习惯编码。在第一行第一次到读到变量后,另一个线程会更新这个值,但你只对初始读到的值感兴趣。

Also, even when the member variable in question is not volatile but final, this idiom has to do with CPU caches as reading from a stack location is more cache-friendly than reading from a random heap location. There is also a higher chance that the local var will end up bound to a CPU register.
另外,即使我们讨论的成员变量不是 volatile而是final, 这个习惯用法也与CPU缓存有关:从栈读变量比从堆读变量会更cache-friendly,本地变量最终绑定到CPU寄存器的可能性更高。

For this latter case there is actually some controversy, since the JIT compiler will usually take care of those concerns, but Doug Lea is one of the guys who sticks with it on general principle.
对于后一种情况,实际上存在一些争议,因为JIT编译器通常会处理这些问题,但是Doug Lea是坚持使用通过原则的人。

  • 速度快
    this.lock 会lock变量多一条汇编指令(需要重新将this 压栈,再通过常量池获取field 字段),在循环的情况下性能提升较明显
 private void stack();
    descriptor: ()V
    flags: ACC_PRIVATE
    Code:
      stack=1, locals=2, args_size=1
         0: aload_0   //将this压线
         1: getfield      #4                  // Field  通过查找常量池获取field内容
 lock:Ljava/util/concurrent/locks/ReentrantLock;
         4: astore_1
         5: aload_0   //重复0行
         6: getfield      #4                  // Field  //重复1行
lock:Ljava/util/concurrent/locks/ReentrantLock;
         9: invokevirtual #8                  // Method java/util/concurrent/locks/ReentrantLock.lock:()V
        12: return
      LineNumberTable:
        line 15: 0
        line 16: 5
        line 17: 12
      LocalVariableTable:
        Start  Length  Slot  Name   Signature
            0      13     0  this   Lcom/sparrow/jdk/local/LocalStackCompare;
            5       8     1  lock   Ljava/util/concurrent/locks/ReentrantLock;
}

private void stack();
    descriptor: ()V
    flags: ACC_PRIVATE
    Code:
      stack=1, locals=2, args_size=1
         0: aload_0
         1: getfield      #4                  // Field lock:Ljava/util/concurrent/locks/ReentrantLock;
         4: astore_1
         5: aload_1  //直接获取本地栈数据
         6: invokevirtual #8                  // Method java/util/concurrent/locks/ReentrantLock.lock:()V
         9: return
      LineNumberTable:
        line 15: 0
        line 16: 5
        line 17: 9
      LocalVariableTable:
        Start  Length  Slot  Name   Signature
            0      10     0  this   Lcom/sparrow/jdk/local/LocalStackCompare;
            5       5     1  lock   Ljava/util/concurrent/locks/ReentrantLock;

测试源码

package com.sparrow.jdk.local;


import java.util.concurrent.locks.ReentrantLock;

public class LocalStackCompare {
    private final ReentrantLock lock = new ReentrantLock();

    public static void main(String[] args) {
        LocalStackCompare compare = new LocalStackCompare();
        compare.stack();
    }

    private void stack() {
        final ReentrantLock lock = this.lock;
        this.lock.lock();
    }
}

概念澄清

很多文章提出,工作内存(work memory)就是栈这样会产生很多误解。

为什么JDK代码这样写?final ReentrantLock takeLock = this.takeLock_第1张图片
image.png

原文:
Every thread is defined to have a working memory (an abstraction of caches and registers) in which to store values.
《Concurrent Programming in Java Design Principles and Pattern 》(P79)

工作内存cacheregister的抽象,不是栈,更不是堆。

为什么JDK代码这样写?final ReentrantLock takeLock = this.takeLock_第2张图片
并发编程艺术原书截图

书中叫本地内存,后边有强调它涵盖了缓存,写缓存区,寄存器以及其他硬件和编译器优化。这句话很重要!

总结

开发过程中尽量将全局变量重赋给局部变量,尤其用局部变量做循环的case
会一定程度上提升性能,也应该是代码的general principle

汇编分析
---

你可能感兴趣的:(为什么JDK代码这样写?final ReentrantLock takeLock = this.takeLock)