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;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值