Java 敞 HashCode
HashCode
算法
长话短说,Java 的 Object.hashCode() 实现算法,据 get_next_hash 所述,可选方案有多种,默认为 5.
> java -XX:+UnlockExperimentalVMOptions -XX:+PrintFlagsFinal -version | findstr hashCode
< intx hashCode = 5 {experimental} {default}
所谓 0 ~ 5,即
- <0>, global Park-Miller RNG
- <1>, 对象内存地址与随机数的异或
- <2>, 始终为1,仅供测试。
- <3>, 全局递增数列
- <4>, 对象内存地址
- <5>, Marsaglia's xor-shift scheme with thread-specific state
时机
Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer.
首次调用时,非创建时。首次调用场景包含多线程下和加锁下。
更多
- src/hotspot/share/runtime/synchronizer.cpp
- Switch to optimal identity hash code generator - <https://bugs.openjdk.org/browse/JDK-8006176>
- hashCode, 一个实验引发的思考 - <https://zhuanlan.zhihu.com/p/28270828>
- Java中的hashCode()是如何实现的? - tyt2222008 - <https://blog.csdn.net/kevin_ut/article/details/8177783>