栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Java中的“内部地址”是什么?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java中的“内部地址”是什么?

这显然是特定于实现的。

下面,我包括

Object.hashCode()
OpenJDK 7中使用的实现。

该函数支持六种不同的计算方法,其中只有两种可以注意到对象的地址(“地址”是C ++

oop
强制转换为
intptr_t
)。两种方法之一按原样使用地址,而另一种方法则进行一些打乱,然后使用不经常更新的随机数将结果混搭。

在其余方法中,一个返回一个常量(大概是为了测试),一个返回序号,其余的则基于伪随机序列。

似乎可以在运行时选择该方法,默认似乎是方法0,即

os::random()
。后者是线性同余生成器,带有所谓的竞争条件。:-)竞争条件是可以接受的,因为在最坏的情况下,它将导致两个对象共享相同的哈希码。这不会破坏任何不变性。

第一次需要哈希码时执行计算。为了保持一致性,结果将存储在对象的标头中,并在随后的调用中返回

hashCode()
。缓存是在此功能之外完成的。

总而言之,

Object.hashCode()
基于对象地址的概念在很大程度上是一个历史性的手工艺品,已被现代垃圾收集器的特性所淘汰。

// hotspot/src/share/vm/runtime/synchronizer.hpp// hashCode() generation ://// Possibilities:// * MD5Digest of {obj,stwRandom}// * CRC32 of {obj,stwRandom} or any linear-feedback shift register function.// * A DES- or AES-style SBox[] mechanism// * One of the Phi-based schemes, such as://   2654435761 = 2^32 * Phi (golden ratio)//   HashCodevalue = ((uintptr_t(obj) >> 3) * 2654435761) ^ GVars.stwRandom ;// * A variation of Marsaglia's shift-xor RNG scheme.// * (obj ^ stwRandom) is appealing, but can result//   in undesirable regularity in the hashCode values of adjacent objects//   (objects allocated back-to-back, in particular).  This could potentially//   result in hashtable collisions and reduced hashtable efficiency.//   There are simple ways to "diffuse" the middle address bits over the//   generated hashCode values://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 = 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 = 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 ;  }  value &= markOopDesc::hash_mask;  if (value == 0) value = 0xBAD ;  assert (value != markOopDesc::no_hash, "invariant") ;  TEVENT (hashCode: GENERATE) ;  return value;}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/427844.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号