3 hashCode的底细

tips:面试常问/罕用/常出错

hashCode到底是什么?是不是对象的内存地址?

1) 间接用内存地址?

指标:通过一个Demo验证这个hasCode到底是不是内存地址

public native int hashCode(); 

com.hashcode.HashCodeTest

package com.hashcode;import org.openjdk.jol.vm.VM;import java.util.ArrayList;import java.util.List;public class HashCodeTest {    //指标:只有产生反复,阐明hashcode不是内存地址,但还须要证实(JVM代码证实)    public static void main(String[] args) {        List<Integer> integerList = new ArrayList<Integer>();        int num = 0;        for (int i = 0; i < 150000; i++) {            //创立新的对象            Object object = new Object();            if (integerList.contains(object.hashCode())) {                num++;//产生反复(内存地址必定不会反复)            } else {                integerList.add(object.hashCode());//没有反复            }        }        System.out.println(num + "个hashcode产生反复");        System.out.println("List共计大小" + integerList.size() + "个");    }}

15万个循环,产生了反复,阐明hashCode不是内存地址(严格的说,必定不是间接取的内存地址)

思考一下,为什么不能间接用内存地址呢?

  • 提醒:jvm垃圾收集算法,对象迁徙……

那么它到底是什么?如何生成的呢

2) 不是地址那在哪里?

既然不是内存地址,那肯定在某个中央存着,那在哪里存着呢?

答案:在对象头里!(画图。类在jvm内存中的布局)

对象头分为两局部,一部分是下面指向class形容的地址Klass,另一部分就是Markword

而咱们这里要找的hashcode在Markword里!(标记位意义,不必记!)

32位:

64位:

3) 什么时候生成的?

new的霎时就有hashcode了吗??

show me the code!咱们用代码验证

package com.hashcode;import org.openjdk.jol.info.ClassLayout;import org.openjdk.jol.vm.VM;public class ShowHashCode {        public static void main(String[] args) {            ShowHashCode a = new ShowHashCode();            //jvm的信息            System.out.println(VM.current().details());            System.out.println("-------------------------");            //调用之前打印a对象的头信息            //以表格的模式打印对象布局            System.out.println(ClassLayout.parseInstance(a).toPrintable());            System.out.println("-------------------------");            //调用后再打印a对象的hashcode值            System.out.println(Integer.toHexString(a.hashCode()));            System.out.println(ClassLayout.parseInstance(a).toPrintable());            System.out.println("-------------------------");            //有线程加重量级锁的时候,再来看对象头            new Thread(()->{                try {                    synchronized (a){                        Thread.sleep(5000);                    }                } catch (InterruptedException e) {                    e.printStackTrace();                }            }).start();            System.out.println(Integer.toHexString(a.hashCode()));            System.out.println(ClassLayout.parseInstance(a).toPrintable());        }}

后果剖析

论断:在你没有调用的时候,这个值是空的,当第一次调用hashCode办法时,会生成,加锁当前,不晓得去哪里了……

4) 怎么生成的?

接上文 , 咱们查究一下,它具体的生成及挪动过程。

咱们都晓得,这货是个本地办法

public native int hashCode();

那就须要借助下面提到的方法,通过JVM虚拟机源码,查看hashcode的生成

1)先从Object.c开始找hashCode映射

src\share\native\java\lang\Object.c

JNIEXPORT void JNICALL//jni调用//全门路:java_lang_Object_registerNatives是java对应的包下办法Java_java_lang_Object_registerNatives(JNIEnv *env, jclass cls){     //jni环境调用;上面的参数methods对应的java办法    (*env)->RegisterNatives(env, cls,                            methods, sizeof(methods)/sizeof(methods[0]));}

JAVA--------------------->C++函数对应

//JAVA办法(返回值)----->C++函数对象static JNINativeMethod methods[] = {    //JAVA办法        返回值  (参数)                          c++函数    {"hashCode",    "()I",                    (void *)&JVM_IHashCode},    {"wait",        "(J)V",                   (void *)&JVM_MonitorWait},    {"notify",      "()V",                    (void *)&JVM_MonitorNotify},    {"notifyAll",   "()V",                    (void *)&JVM_MonitorNotifyAll},    {"clone",       "()Ljava/lang/Object;",   (void *)&JVM_Clone},};

JVM_IHashCod在哪里呢?

2)全局检索JVM_IHashCode

齐全搜不到这个办法名,只有这个还对付有点像,那这是个啥呢?

src\share\vm\prims\jvm.cpp

/*JVM_ENTRY is a preprocessor macro thatadds some boilerplate code that is common for all functions of HotSpot JVM API.This API is a connection layer between the native code of JDK class library and the JVM.JVM_ENTRY是一个预加载宏,减少一些样板代码到jvm的所有function中这个api是位于本地办法与jdk之间的一个连贯层。所以,此处才是生成hashCode的逻辑!*/JVM_ENTRY(jint, JVM_IHashCode(JNIEnv* env, jobject handle))  JVMWrapper("JVM_IHashCode");  //调用了ObjectSynchronizer对象的FastHashCode return handle == NULL ? 0 : ObjectSynchronizer::FastHashCode (THREAD, JNIHandles::resolve_non_null(handle)) ;JVM_END

3)持续,ObjectSynchronizer::FastHashCode

先说生成流程,留个印象:

intptr_t ObjectSynchronizer::FastHashCode (Thread * Self, oop obj) {    //是否开启了偏差锁(Biased:偏差,偏向)  if (UseBiasedLocking) {    //如果以后对象处于偏差锁状态    if (obj->mark()->has_bias_pattern()) {      Handle hobj (Self, obj) ;      assert (Universe::verify_in_progress() ||              !SafepointSynchronize::is_at_safepoint(),             "biases should not be seen by VM thread here");            //那么就撤销偏差锁(达到无锁状态,revoke:破除)      BiasedLocking::revoke_and_rebias(hobj, false, JavaThread::current());      obj = hobj() ;          //断言下,看看是否撤销胜利(撤销后为无锁状态)      assert(!obj->mark()->has_bias_pattern(), "biases should be revoked by now");    }  }  // ……  ObjectMonitor* monitor = NULL;  markOop temp, test;  intptr_t hash;  //读出一个稳固的mark;避免对象obj处于收缩状态;  //如果正在收缩,就等他收缩结束再读出来  markOop mark = ReadStableMark (obj);    //是否撤销了偏差锁(也就是无锁状态)(neutral:中立,不偏不斜的)  if (mark->is_neutral()) {    //从mark头上取hash值    hash = mark->hash();        //如果有,间接返回这个hashcode(xor)    if (hash) {                       // if it has hash, just return it      return hash;    }        //如果没有就新生成一个(get_next_hash)    hash = get_next_hash(Self, obj);  // allocate a new hash code    //生成后,原子性设置,将hash放在对象头里去,这样下次就能够间接取了    temp = mark->copy_set_hash(hash); // merge the hash code into header    // use (machine word version) atomic operation to install the hash    test = (markOop) Atomic::cmpxchg_ptr(temp, obj->mark_addr(), mark);    if (test == mark) {      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.    //如果曾经升级成了重量级锁,那么找到它的monitor    //也就是咱们所说的内置锁(objectMonitor),这是c里的数据类型    //因为锁降级后,mark里的bit位曾经不再存储hashcode,而是指向monitor的地址    //而降级的markword呢?被移到了c的monitor里  } else if (mark->has_monitor()) {    //沿着monitor找header,也就是对象头    monitor = mark->monitor();    temp = monitor->header();    assert (temp->is_neutral(), "invariant") ;    //找到header后取hash返回    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())) {    //轻量级锁的话,也是从java对象头移到了c里,叫helper    temp = mark->displaced_mark_helper(); // this is a lightweight monitor owned    assert (temp->is_neutral(), "invariant") ;    hash = temp->hash();              // by current thread, check if the displaced    //找到,返回    if (hash) {                       // header contains hash code      return hash;    }  }        ......略

问:

为什么要先撤销偏差锁到无锁状态,再来生成hashcode呢?这跟锁有什么关系?

答:

mark word里,hashcode存储的字节地位被偏差锁给占了!偏差锁存储了锁持有者的线程id

(参考下面的markword图)

扩大:对于hashCode的生成算法(理解)

// hashCode() generation :// 波及到c++算法畛域,感兴趣的同学自行钻研// 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<intptr_t>(obj) >> 3 ;     value = addrBits ^ (addrBits >> 5) ^ GVars.stwRandom ;//随机数位移异或计算  } else  if (hashCode == 2) {     value = 1 ;            // 返回1  } else  if (hashCode == 3) {     value = ++GVars.hcSequence ;//返回一个Sequence序列号  } else  if (hashCode == 4) {     value = cast_from_oop<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.     //马萨利亚传授写的xor-shift 随机数算法(异或随机算法)     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 ;  }

5)总结

通过剖析虚拟机源码咱们证实了hashCode不是间接用的内存地址,而是采取肯定的算法来生成

hashcode值的存储在mark word里,与锁共用一段bit位,这就造成了跟锁状态相关性

  • 如果是偏差锁:

一旦调用hashcode,偏差锁将被撤销,hashcode被保留占位mark word,对象被打回无锁状态

  • 那偏偏这会就是有线程硬性应用对象的锁呢?

对象再也回不到偏差锁状态而是降级为重量级锁。hash code追随mark word被挪动到c的object monitor,从那里取

本文由传智教育博学谷 - 狂野架构师教研团队公布,转载请注明出处!

如果本文对您有帮忙,欢送关注和点赞;如果您有任何倡议也可留言评论或私信,您的反对是我保持创作的能源