对于没有覆盖hashCode()方法的对象
如果没有覆盖 hashCode() 方法,那么哈希值为底层 JDK C++ 源码实现,实例每次调用hashcode()方法,只有第一次计算哈希值,之后哈希值会存储在对象头的 标记字(MarkWord) 中。

如果进入各种锁状态,那么会缓存在其他地方,一般是获取锁的线程里面存储,恢复无锁(即释放锁)会改回原有的哈希值。
对应源码synchronizer.cpp:
1//如果是无锁状态 2if (mark.is_neutral()) { 3 hash = mark.hash(); 4 //如果hash不等于0,证明计算过,直接返回 5 if (hash != 0) { 6 return hash; 7 } 8 //否则,计算hash值 9 hash = get_next_hash(self, obj); // get a new hash 10 //拷贝到Header中记录 11 temp = mark.copy_set_hash(hash); 12 test = obj->cas_set_mark(temp, mark); 13 //可能有并发,而且不同默认哈希值计算方法,可能每次哈希值不一样,只有 CAS 成功的才是最后的哈希值 14 //默认的哈希值计算,不论计算多少次,都不会变 15 if (test == mark) { 16 return hash; 17 } 18} else if (mark.has_monitor()) { 19 //如果是有 monitor 锁状态(重量级锁),则获取其 monitor,哈希值会记录在monitor的头部 20 monitor = mark.monitor(); 21 temp = monitor->header(); 22 assert(temp.is_neutral(), "invariant: header=" INTPTR_FORMAT, temp.value()); 23 hash = temp.hash(); 24 if (hash != 0) { 25 OrderAccess::loadload_for_IRIW(); 26 if (monitor->is_being_async_deflated()) { 27 monitor->install_displaced_markword_in_object(obj); 28 continue; 29 } 30 return hash; 31 } 32} else if (self->is_lock_owned((address)mark.locker())) { 33 // 如果是轻量级锁状态,获取轻量锁,其中也记录着之前计算的哈希值 34 temp = mark.displaced_mark_helper(); 35 assert(temp.is_neutral(), "invariant: header=" INTPTR_FORMAT, temp.value()); 36 hash = temp.hash(); 37 if (hash != 0) { // if it has a hash, just return it 38 return hash; 39 } 40}
对于已经覆盖hashCode()方法的对象
对于已经覆盖hashCode()方法的对象,则每次都会重新调用hashCode()方法重新计算哈希值。
微信搜索“我的编程喵”关注公众号,每日一刷,轻松提升技术,斩获各种offer:
