美文网首页
java之hashcode方法

java之hashcode方法

作者: 一万年不是尽头 | 来源:发表于2021-11-04 09:57 被阅读0次

talk is cheap,直接上本地方法的最终实现


static inline intptr_t get_next_hash(Thread* current, oop obj) {

  intptr_t value = 0;

  if (hashCode == 0) {

    // This form uses global Park-Miller RNG.

    // On MP system we'll have lots of RW access to a global, so the

    // mechanism induces lots of coherency traffic.

    value = os::random();

  } else if (hashCode == 1) {

    // This variation has the property of being stable (idempotent)

    // between STW operations.  This can be useful in some of the 1-0

    // synchronization schemes.

    intptr_t addr_bits = cast_from_oop<intptr_t>(obj) >> 3;

    value = addr_bits ^ (addr_bits >> 5) ^ GVars.stw_random;

  } else if (hashCode == 2) {

    value = 1;            // for sensitivity testing

  } else if (hashCode == 3) {

    value = ++GVars.hc_sequence;

  } else if (hashCode == 4) {

    value = cast_from_oop<intptr_t>(obj);

  } else {

    // Marsaglia's xor-shift scheme with thread-specific state

    // This is probably the best overall implementation -- we'll

    // likely make this the default in future releases.

    unsigned t = current->_hashStateX;

    t ^= (t << 11);

    current->_hashStateX = current->_hashStateY;

    current->_hashStateY = current->_hashStateZ;

    current->_hashStateZ = current->_hashStateW;

    unsigned v = current->_hashStateW;

    v = (v ^ (v >> 19)) ^ (t ^ (t >> 8));

    current->_hashStateW = v;

    value = v;

  }

  value &= markWord::hash_mask;

  if (value == 0) value = 0xBAD;

  assert(value != markWord::no_hash, "invariant");

  return value;

}

相关文章

网友评论

      本文标题:java之hashcode方法

      本文链接:https://www.haomeiwen.com/subject/zqghzltx.html