• 关于 ConcurrentHashMap 1.8 中的线程探针哈希(ThreadLocalRandom.getProbe)


    ConcurrentHashMap 在累加键值对个数的 addCount 函数中,使用 ThreadLocalRandom.getProbe() 得到线程的探针哈希值。

    在这里,这个探针哈希值的作用是哈希线程,将线程和数组中的不用元素对应起来,尽量避免线程争用同一数组元素。探针哈希值和 map 里使用的哈希值的区别是,当线程发生数组元素争用后,可以改变线程的探针哈希值,让线程去使用另一个数组元素,而 map 中 key 对象的哈希值,由于有定位 value 的需求,所以它是一定不能变的。

    那么这个探针哈希值是在哪计算的呢?带着这个问题我们继续往下看。
    ThreadLocalRandom.getProbe() 方法如下:

    /**
     * Returns the probe value for the current thread without forcing
     * initialization. Note that invoking ThreadLocalRandom.current()
     * can be used to force initialization on zero return.
     */
    static final int getProbe() {
        return UNSAFE.getInt(Thread.currentThread(), PROBE);
      }
    

    PROBE 是什么?

      // Unsafe mechanics
      private static final sun.misc.Unsafe UNSAFE;
      ...
      private static final long PROBE;
      ...
      static {
          try {
              UNSAFE = sun.misc.Unsafe.getUnsafe();
              Class<?> tk = Thread.class;
              ...
              PROBE = UNSAFE.objectFieldOffset
                  (tk.getDeclaredField("threadLocalRandomProbe"));
              ...
          } catch (Exception e) {
              throw new Error(e);
            }
        }  
    

    可以看到 PROBE 表示的是 Thread 类 threadLocalRandomProbe 字段的偏移量。
    所以 getProbe 方法的功能就是简单的返回当前线程 threadLocalRandomProbe 字段的值。

    接着去 Thread 类看看这个 threadLocalRandomProbe 字段,

      /** Probe hash value; nonzero if threadLocalRandomSeed initialized */
      @sun.misc.Contended("tlr")
      int threadLocalRandomProbe;
    

    Thread 类仅仅是定义了这个字段,并没有将其初始化,其初始化工作由 ThreadLocalRandom 类来做。
    ThreadLocalRandom 类的 localInit 方法完成初始化工作,

      /**
       * Initialize Thread fields for the current thread.  Called only
       * when Thread.threadLocalRandomProbe is zero, indicating that a
       * thread local seed value needs to be generated. Note that even
       * though the initialization is purely thread-local, we need to
       * rely on (static) atomic generators to initialize the values.
       */
      static final void localInit() {
      	// probeGenerator 是一个 AtomicInteger 类型
      	// PROBE_INCREMENT 是一个静态常量,值为 0x9e3779b9
          int p = probeGenerator.addAndGet(PROBE_INCREMENT);
          int probe = (p == 0) ? 1 : p; // skip 0
          long seed = mix64(seeder.getAndAdd(SEEDER_INCREMENT));
          Thread t = Thread.currentThread(); // 获取当前线程
          // 通过 Unsafe 对象初始化当前线程的 threadLocalRandomSeed 字段
          UNSAFE.putLong(t, SEED, seed);
          // 通过 Unsafe 对象初始化当前线程的 threadLocalRandomProbe 字段
          UNSAFE.putInt(t, PROBE, probe);
      }
    

    SEED 和 PROBE 类似,它表示的是 Thread 类 threadLocalRandomSeed 字段的偏移量。

    在 ThreadLocalRandom 类的这个 localInit 方法里,同时初始化了当前线程的 threadLocalRandomSeed 字段和 threadLocalRandomProbe 字段。

    所以在 Thread 类 threadLocalRandomProbe 字段上的注释中说:nonzero if threadLocalRandomSeed initialized。就是说如果 threadLocalRandomSeed 字段被初始化了,threadLocalRandomProbe 字段就非零。因为它俩是同时被初始化的。

    除此之外,也可以通过 ThreadLocalRandom 类的 advanceProbe 方法更改当前线程 threadLocalRandomProbe 的值。

      /**
       * Pseudo-randomly advances and records the given probe value for the
       * given thread.
       */
      static final int advanceProbe(int probe) {
          probe ^= probe << 13;   // xorshift
          probe ^= probe >>> 17;
          probe ^= probe << 5;
          UNSAFE.putInt(Thread.currentThread(), PROBE, probe);
          return probe;
      }
    

    ConcurrentHashMap 里的 fullAddCount 方法会调用 ThreadLocalRandom.localInit() 初始化当前线程的探针哈希值;当发生线程争用后,也会调用 ThreadLocalRandom.advanceProbe(h) 更改当前线程的探针哈希值,

       private final void fullAddCount(long x, boolean wasUncontended) {
          int h;
          if ((h = ThreadLocalRandom.getProbe()) == 0) {
              ThreadLocalRandom.localInit();      // force initialization
              h = ThreadLocalRandom.getProbe();
              wasUncontended = true;
          }
          ...
          h = ThreadLocalRandom.advanceProbe(h);
          ...
      }
    

    原文链接:https://blog.csdn.net/L__ear/article/details/106528892

  • 相关阅读:
    面试再问ThreadLocal,别说你不会
    利用 Docker Compose 搭建 SpringBoot 运行环境(超详细步骤和分析)
    高并发场景下缓存处理的一些思路!
    利用Dockerfile部署SpringBoot项目
    [RH254] 1-运行级别
    [RH134] 12-系统启动
    [安全] HTTPS的理解
    [工具] Wireshark与相关的网络安全
    [Python自学] 爬虫(5)selenium
    [Python自学] 爬虫(4)xpath
  • 原文地址:https://www.cnblogs.com/snail-gao/p/13605061.html
Copyright © 2020-2023  润新知