hashcode java_java 的Object类的hashcode()方法具体是怎么实现的?
轻松解说Object.hashcode()的实现,让你看着不累!
intptr_t ObjectSynchronizer::FastHashCode (Thread * Self, oop obj) {
// 如果启用偏向锁
if (UseBiasedLocking) {
// NOTE: many places throughout the JVM do not expect a safepoint
// to be taken here, in particular most operations on perm gen
// objects. However, we only ever bias Java instances and all of
// the call sites of identity_hash that might revoke biases have
// been checked to make sure they can handle a safepoint. The
// added check of the bias pattern is to avoid useless calls to
// thread-local storage.
// 如果对象处于“已偏向”状态
if (obj->mark()->has_bias_pattern()) {
// Box and unbox the raw reference just in case we cause a STW safepoint.
// 将obj对象包装成一个句柄->hobj
Handle hobj (Self, obj) ;
// Relaxing assertion for bug 6320749.
// 保证程序的执行条件
assert (Universe::verify_in_progress() ||
!SafepointSynchronize::is_at_safepoint(),
"biases should not be seen by VM thread here");
// 撤销偏向锁
BiasedLocking::revoke_and_rebias(hobj, false, JavaThread::current());
// 这里根据Handle类定义的无参函数对象,将obj再取出来
obj = hobj() ;
// 看看是不是确保成功撤销了
assert(!obj->mark()->has_bias_pattern(), "biases should be revoked by now");
}
}
// hashCode() is a heap mutator ...
// Relaxing assertion for bug 6320749.
/**
* 1. 确保当前的执行路径不处在全局安全点上;
* 2. 确保当前线程是个JavaThread
* 3. 确保当前线程没有被block
*/
assert (Universe::verify_in_progress() ||
!SafepointSynchronize::is_at_safepoint(), "invariant") ;
assert (Universe::verify_in_progress() ||
Self->is_Java_thread() , "invariant") ;
assert (Universe::verify_in_progress() ||
((JavaThread *)Self)->thread_state() != _thread_blocked, "invariant") ;
ObjectMonitor* monitor = NULL;
markOop temp, test;
intptr_t hash;
// 读出一个稳定的mark,防止obj正处于锁膨胀状态,如果正在膨胀,就等它膨胀完,再读出来
markOop mark = ReadStableMark (obj);
// object should remain ineligible for biased locking
// 我擦,你不会还处于“已偏向”状态吧
assert (!mark->has_bias_pattern(), "invariant") ;
// 如果mark现在处于中立状态了->unlock 这时候mark的结构应该是 [hash|age|0|01]
if (mark->is_neutral()) {
// 看看mark中是不是有一个存在的hash值
hash = mark->hash(); // this is a normal header
// 我靠,有了,省事了,直接返回吧
if (hash) { // if it has hash, just return it
return hash;
}
// 没有,那我就根据hashCode的算法规则重新算一个出来吧
hash = get_next_hash(Self, obj); // allocate a new hash code
// 把上面的hash结果merge到mark中去,看到我写的那个结构了吗,放到hash那个位置
// 得到一个临时的temp,为什么这么干,继续看下面
temp = mark->copy_set_hash(hash); // merge the hash code into header
// use (machine word version) atomic operation to install the hash
// 上面的E文注释写得已经很直白了,CAS安装hash值咯
test = (markOop) Atomic::cmpxchg_ptr(temp, obj->mark_addr(), mark);
if (test == mark) {
// 看来CAS操作成功了,返回hash
return hash;
}
// If atomic operation failed, we must inflate the header
// into heavy weight monitor. We could add more code here
// for fast path, but it does not worth the complexity.
// 妈的,CAS失败了,上面E文说了,我们要把对象头膨胀成重量级锁了!
// 看看重锁状态时,mark的结构吧- monitor_ptr|10
// Anyway, 看看现在你是不是已经是重锁状态了吧
} else if (mark->has_monitor()) {
// 好家伙,你已经膨胀成重锁了嘛
monitor = mark->monitor();
// 那么我们就从ObjectMonitor对象中将mark取出来看看吧
temp = monitor->header();
// 这时候mark应该是无锁中立状态了,结构看上面吧!
assert (temp->is_neutral(), "invariant") ;
// 完事OK,取出来返回吧!
hash = temp->hash();
if (hash) {
return hash;
}
// Skip to the following code to reduce code size
// 锁对象正处在轻量级锁的状态,并且锁的持有者还是当前线程呢!
} else if (Self->is_lock_owned((address)mark->locker())) {
// 直接从线程栈里把mark取出来吧
temp = mark->displaced_mark_helper(); // this is a lightweight monitor owned
// mark不是中立状态?你搞笑吧!
assert (temp->is_neutral(), "invariant") ;
// 取出来返回咯
hash = temp->hash(); // by current thread, check if the displaced
if (hash) { // header contains hash code
return hash;
}
// WARNING:
// The displaced header is strictly immutable.
// It can NOT be changed in ANY cases. So we have
// to inflate the header into heavyweight monitor
// even the current thread owns the lock. The reason
// is the BasicLock (stack slot) will be asynchronously
// read by other threads during the inflate() function.
// Any change to stack may not propagate to other threads
// correctly.
}
// 苦逼地等待你膨胀成重锁了...
// Inflate the monitor to set hash code
monitor = ObjectSynchronizer::inflate(Self, obj);
// Load displaced header and check it has hash code
// 从重锁对象中load出对象头mark来,看看是否已经有了一个hash值了
mark = monitor->header();
// check 不解释了
assert (mark->is_neutral(), "invariant") ;
hash = mark->hash();
// hash值还是空的,well,我们还是算一个出来吧!
// 下面的逻辑跟上面的一段一致,哥就不用费口舌了...
if (hash == 0) {
hash = get_next_hash(Self, obj);
temp = mark->copy_set_hash(hash); // merge hash code into header
assert (temp->is_neutral(), "invariant") ;
test = (markOop) Atomic::cmpxchg_ptr(temp, monitor, mark);
if (test != mark) {
// The only update to the header in the monitor (outside GC)
// is install the hash code. If someone add new usage of
// displaced header, please update this code
hash = test->hash();
assert (test->is_neutral(), "invariant") ;
assert (hash != 0, "Trivial unexpected object/monitor header usage.");
}
}
// We finally get the hash
// 费了好大劲,终于拿到hash值了,鸡冻~
return hash;
}
上面还有一个重要的函数get_next_hash, hachCode是hash的生成策略,默认值是5,可在虚拟机启动时配置(由于算法比较严肃,轻松不了了~~):
// 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 = cast_from_oop(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(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;
}
hashcode java_java 的Object类的hashcode()方法具体是怎么实现的?相关推荐
- Java基础知识强化26:Object类之hashCode()方法、getClass()方法
1. Object类的hashCode()方法,如下: public int hashCode():返回该对象的哈希码值,这个值和地址值有关,但是不是实际地址值(哈希码值是根据实际地址值转化过来的 ...
- Object类中有哪些方法
2019独角兽企业重金招聘Python工程师标准>>> Object类中的方法介绍 类Object是类层次结构的根类,每一个类都使用Object作为超类,所有对象(包括数组)都实现这 ...
- 重写Object类的equals方法
package com.learn.demo01.Object;import java.util.ArrayList;public class Demo02Equals {public static ...
- Object类的toString方法
package com.learn.demo01.Object;import java.util.ArrayList; import java.util.Random; import java.uti ...
- JAVA进阶教学之(Object类的toString方法)
1.toString public String toString() 返回对象的字符串表示形式.总的来说,这 toString方法返回一个字符串,"以文本方式表示"这个对象.其结 ...
- [ 转载 ] Java基础10--关于Object类下所有方法的简单解析
关于Object类下所有方法的简单解析 类Object是类层次结构的根类,是每一个类的父类,所有的对象包括数组,String,Integer等包装类,所以了解Object是很有必要的,话不多说,我们直 ...
- Java面试题:final和Object类常见的方法
final 关键字的理解 final关键字主要用在三个地方:变量.方法.类. 对于一个final变量,如果是基本数据类型的变量,则其数值一旦在初始化之后便不能更改:如果是引用类型的变量,则在对其初始化之 ...
- Object类的各个方法及其作用
Object类的各个方法及其作用 一.简介Object类 1.Object类是所有类的父类,即每个类都直接或简介继承自该类.所以一个Object类型的变量可以引用任何对象,不论是类实例还是数组. ...
- JavaSE学习总结(八)常用类(上)Object类==与equals方法的区别浅克隆的特点Scanner类String类String两种创建对象方式的区别String类的各种功能
JavaSE学习总结(八)常用类(上)/Object类/==与equals方法的区别/浅克隆的特点/Scanner类/String类/String两种创建对象方式的区别/String类的各种功能 常用 ...
最新文章
- 层次聚类算法原理总结
- AI芯片重磅破局者,开启边缘新智元
- 用python重复下载文件_python 爬虫 重复下载 二次请求
- 微信实现定位城市并获取城市编码
- 准备 KVM 实验环境 - 每天5分钟玩转 OpenStack(3)
- ORACLE 中ROWNUM用法总结!
- pat乙级相当于什么水平_英语四六级/专四/专八相当于美国人什么水平?
- linux怎样进入src目录,Linux配置目录
- Mybatis 开发神器:Fast MyBatis,太强了!
- 城市电子地图站点 推荐
- springboot接入微信,支付宝支付
- 剑指offer_33:输入一个正整数数组,把数组里所有数字拼接起来排成一个数,打印能拼接出的所有数字中最小的一个。例如输入数组{3,32,321},则打印出这三个数字能排成的最小数字为321323。
- 麒麟V10 kylin v10服务器版yum软件源官方源亲测可用
- Ubuntu 编译XCB源码
- keil 生成三角波dac0832_怎么样利用南方CASS三角网法和方格网法进行土方量计算...
- xftp无法显示远程文件夹
- 计算机对操作系统函数的调用失败,解决win7提示“远程过程调用失败且未执行”的方案...
- Tomcat网站服务
- STM32使用串口中断接收HWT101的数据
- securecrt 不掉线_如何不掉线
热门文章
- 《面向对象分析与设计》一第2章 什么是面向对象分析
- linux命令积累之egrep命令
- ×××常见问题原因与解决
- linux无法设置变量,linux – crontab在作业之前无法设置变量
- 正则表达式matlab,正则表达式中一个word的匹配 @MATLAB - 优秀的Free OS(Linux)版 - 北大未名BBS...
- leetcode94. 二叉树的中序遍历(dfs)
- sphinx 项目根目录_如何使用Sphinx工具记录Django项目
- 决策者根据什么曲线做出决策_如何做出产品设计决策
- 数据草拟:使您的团队热爱数据的研讨会
- matplotlib柱状图、面积图、直方图、散点图、极坐标图、箱型图