Java 基础

总结

答案:不一定。不同的策略,hashcode返回值会不一样。

1-根据Object.hashcode()的注释,说明hashCode返回值与对象内存地址有一定关系。但只是其中一种策略而已。

2-根据c++ native hashcode()的源码,通过一个JVM启动参数-XX:hashCode,可以任意切换hashCode的生成策略:

  • 与地址相关的生成策略有两条:
    • hashCode==1:这种方式具有幂等的性质,在STW(stop-the-world)操作中,这种策略通常用于同步方案中。利用对象地址计算,使用不经常更新的随机数参与运算。
    • hashCode==4:与创建对象的内存位置有关,原样输出。
  • 其他情况:
    • hashCode==0:简单地返回随机数,与对象的内存地址没有联系。然而根据随机数生成并全局地读写在多处理器下并不占优势。
    • hashCode==2:始终返回完全相同的标识,即hashCode=1。这可用于测试依赖对象标识的代码。
    • hashcode==3:从零开始计算哈希代码值。它看起来不是线程安全的,因此多个线程可以生成具有相同哈希代码的对象。
    • hashCode>=5(默认):在jdk1.8中,这是默认的hashCode生成算法,支持多线程生成。使用了Marsaglia的xor-shift算法产生伪随机数。

c++ hashcode() 源码

static inline intptr_t get_next_hash(Thread * Self, oop obj) {
  intptr_t value = 0 ;
  if (hashCode == 0) {
     // This form uses an unguarded global Park-Miller RNG,
     // so it's possible for two threads to race and generate the same 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 addrBits = cast_from_oop<intptr_t>(obj) >> 3 ;
     value = addrBits ^ (addrBits >> 5) ^ GVars.stwRandom ;
  } else
  if (hashCode == 2) {
     value = 1 ;            // for sensitivity testing
  } else
  if (hashCode == 3) {
     value = ++GVars.hcSequence ;
  } 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 = Self->_hashStateX ;
     t ^= (t << 11) ;
     Self->_hashStateX = Self->_hashStateY ;
     Self->_hashStateY = Self->_hashStateZ ;
     Self->_hashStateZ = Self->_hashStateW ;
     unsigned v = Self->_hashStateW ;
     v = (v ^ (v >> 19)) ^ (t ^ (t >> 8)) ;
     Self->_hashStateW = v ;
     value = v ;
  }

参考文献

hashcode值一样对象一定相同吗_你所不知道的HashCode

原文地址:https://www.cnblogs.com/frankcui/p/14375711.html