zoukankan      html  css  js  c++  java
  • 关于 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

  • 相关阅读:
    《TCP/IP 详解 卷1:协议》第 10 章:用户数据报协议
    《TCP/IP 详解 卷1:协议》第 9 章:广播和本地组播(IGMP 和 MLD)
    在新的电脑上部署 Hexo,保留原有博客的方法
    当你不知道变量类型的完整定义时可以采取的操作
    python learning GUI
    python learning Network Programming.py
    python learning Process and Thread.py
    【2017级面向对象程序设计】第2次成绩排行
    python learning IO.py
    python learning Exception & Debug.py
  • 原文地址:https://www.cnblogs.com/snail-gao/p/13605061.html
Copyright © 2011-2022 走看看