[Java基础要义] Java语言中Object对象的hashCode()取值的底层算法是怎样实现的?

来源:互联网 发布:js表单提交到数据库 编辑:程序博客网 时间:2024/05/22 16:40

 Java语言中,Object对象有个特殊的方法:hashcode(), hashcode()表示的是JVM虚拟机为这个Object对象分配的一个int类型的数值,JVM会使用对象的hashcode值来提高对HashMap、Hashtable哈希表存取对象的使用效率。

      关于Object对象的hashCode()返回值,网上对它就是一个简单的描述:“JVM根据某种策略生成的”,那么这种策略到底是什么呢?我有一个毛病,遇到这种含糊其辞的东西,就想探个究竟,所以,本文就将hashCode()本地方法的实现给扒出来,也给大家在了解hashCode()的过程中提供一点点帮助吧。

      本文将根据openJDK 7源码,向展示Java语言中的Object对象的hashCode() 生成的神秘面纱,我将一步一步地向读者介绍Java Object 的hashcode()方法到底底层调用了什么函数。为了更好地了解这个过程,你可以自己下载openJDK 7 源码,亲自查看和跟踪源码,了解hashCode()的生成过程:

         openJDK 7 下载地址1:http://download.java.net/openjdk/jdk7 (官网,下载速度较慢)

         openJDK 7 下载地址2 :openjdk-7-fcs-src-b147-27_jun_2011.zip (csdn 网友提供的资源,很不错)

        

1.查看openJDK 关于 java.lang.Object类及其hashcode()方法的定义:

   进入openjdk\jdk\src\share\classes\java\lang目录下,可以看到 Object.java源码,打开,查看hashCode()的定义如下所示:

[java] view plain copy
 print?
  1. public native int hashCode();  
   即该方法是一个本地方法,Java将调用本地方法库对此方法的实现。由于Object类中有JNI方法调用,按照JNI的规则,应当生成JNI 的头文件,在此目录下执行javah -jni java.lang.Object 指令,将生成一个java_lang_Object.h头文件,该头文件将在后面用到它

   java_lang_Object.h头文件关于hashcode方法的信息如下所示:

[objc] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. /* 
  2.  * Class:     java_lang_Object 
  3.  * Method:    hashCode 
  4.  * Signature: ()I 
  5.  */  
  6. JNIEXPORT jint JNICALL Java_java_lang_Object_hashCode  
  7.   (JNIEnv *, jobject);  


2. Object对象的hashCode()方法在C语言文件Object.c中实现

  打开openjdk\jdk\src\share\native\java\lang\目录,查看Object.c文件,可以看到hashCode()的方法被注册成有JVM_IHashCode方法指针来处理:

[cpp] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. #include <stdio.h>  
  2. #include <signal.h>  
  3. #include <limits.h>  
  4.   
  5. #include "jni.h"  
  6. #include "jni_util.h"  
  7. #include "jvm.h"  
  8.   
  9. #include "java_lang_Object.h"  
  10.   
  11. static JNINativeMethod methods[] = {  
  12.     {"hashCode",    "()I",                    (void *)&JVM_IHashCode},//hashcode的方法指针JVM_IHashCode  
  13.     {"wait",        "(J)V",                   (void *)&JVM_MonitorWait},  
  14.     {"notify",      "()V",                    (void *)&JVM_MonitorNotify},  
  15.     {"notifyAll",   "()V",                    (void *)&JVM_MonitorNotifyAll},  
  16.     {"clone",       "()Ljava/lang/Object;",   (void *)&JVM_Clone},  
  17. };  
  18.   
  19. JNIEXPORT void JNICALL  
  20. Java_java_lang_Object_registerNatives(JNIEnv *env, jclass cls)  
  21. {  
  22.     (*env)->RegisterNatives(env, cls,  
  23.                             methods, sizeof(methods)/sizeof(methods[0]));  
  24. }  
  25.   
  26. JNIEXPORT jclass JNICALL  
  27. Java_java_lang_Object_getClass(JNIEnv *env, jobject this)  
  28. {  
  29.     if (this == NULL) {  
  30.         JNU_ThrowNullPointerException(env, NULL);  
  31.         return 0;  
  32.     } else {  
  33.         return (*env)->GetObjectClass(env, this);  
  34.     }  
  35. }  

3.JVM_IHashCode方法指针在 openjdk\hotspot\src\share\vm\prims\jvm.cpp中定义,如下:

[cpp] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. JVM_ENTRY(jint, JVM_IHashCode(JNIEnv* env, jobject handle))  
  2.   JVMWrapper("JVM_IHashCode");  
  3.   // as implemented in the classic virtual machine; return 0 if object is NULL  
  4.   return handle == NULL ? 0 : ObjectSynchronizer::FastHashCode (THREAD, JNIHandles::resolve_non_null(handle)) ;  
  5. JVM_END  

  如上可以看出,JVM_IHashCode方法中调用了ObjectSynchronizer::FastHashCode方法

4. ObjectSynchronizer::fashHashCode方法的实现:

     ObjectSynchronizer::fashHashCode()方法在 openjdk\hotspot\src\share\vm\runtime\synchronizer.cpp 文件中实现,其核心代码实现如下所示:

[cpp] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. // hashCode() generation :  
  2. //  
  3. // Possibilities:  
  4. // * MD5Digest of {obj,stwRandom}  
  5. // * CRC32 of {obj,stwRandom} or any linear-feedback shift register function.  
  6. // * A DES- or AES-style SBox[] mechanism  
  7. // * One of the Phi-based schemes, such as:  
  8. //   2654435761 = 2^32 * Phi (golden ratio)  
  9. //   HashCodeValue = ((uintptr_t(obj) >> 3) * 2654435761) ^ GVars.stwRandom ;  
  10. // * A variation of Marsaglia's shift-xor RNG scheme.  
  11. // * (obj ^ stwRandom) is appealing, but can result  
  12. //   in undesirable regularity in the hashCode values of adjacent objects  
  13. //   (objects allocated back-to-back, in particular).  This could potentially  
  14. //   result in hashtable collisions and reduced hashtable efficiency.  
  15. //   There are simple ways to "diffuse" the middle address bits over the  
  16. //   generated hashCode values:  
  17. //  
  18.   
  19. static inline intptr_t get_next_hash(Thread * Self, oop obj) {  
  20.   intptr_t value = 0 ;  
  21.   if (hashCode == 0) {  
  22.      // This form uses an unguarded global Park-Miller RNG,  
  23.      // so it's possible for two threads to race and generate the same RNG.  
  24.      // On MP system we'll have lots of RW access to a global, so the  
  25.      // mechanism induces lots of coherency traffic.  
  26.      value = os::random() ;  
  27.   } else  
  28.   if (hashCode == 1) {  
  29.      // This variation has the property of being stable (idempotent)  
  30.      // between STW operations.  This can be useful in some of the 1-0  
  31.      // synchronization schemes.  
  32.      intptr_t addrBits = intptr_t(obj) >> 3 ;  
  33.      value = addrBits ^ (addrBits >> 5) ^ GVars.stwRandom ;  
  34.   } else  
  35.   if (hashCode == 2) {  
  36.      value = 1 ;            // for sensitivity testing  
  37.   } else  
  38.   if (hashCode == 3) {  
  39.      value = ++GVars.hcSequence ;  
  40.   } else  
  41.   if (hashCode == 4) {  
  42.      value = intptr_t(obj) ;  
  43.   } else {  
  44.      // Marsaglia's xor-shift scheme with thread-specific state  
  45.      // This is probably the best overall implementation -- we'll  
  46.      // likely make this the default in future releases.  
  47.      unsigned t = Self->_hashStateX ;  
  48.      t ^= (t << 11) ;  
  49.      Self->_hashStateX = Self->_hashStateY ;  
  50.      Self->_hashStateY = Self->_hashStateZ ;  
  51.      Self->_hashStateZ = Self->_hashStateW ;  
  52.      unsigned v = Self->_hashStateW ;  
  53.      v = (v ^ (v >> 19)) ^ (t ^ (t >> 8)) ;  
  54.      Self->_hashStateW = v ;  
  55.      value = v ;  
  56.   }  
  57.   
  58.   value &= markOopDesc::hash_mask;  
  59.   if (value == 0) value = 0xBAD ;  
  60.   assert (value != markOopDesc::no_hash, "invariant") ;  
  61.   TEVENT (hashCode: GENERATE) ;  
  62.   return value;  
  63. }  
  64. //   ObjectSynchronizer::FastHashCode方法的实现,该方法最终会返回我们期望已久的hashcode  
  65. intptr_t ObjectSynchronizer::FastHashCode (Thread * Self, oop obj) {  
  66.   if (UseBiasedLocking) {  
  67.     // NOTE: many places throughout the JVM do not expect a safepoint  
  68.     // to be taken here, in particular most operations on perm gen  
  69.     // objects. However, we only ever bias Java instances and all of  
  70.     // the call sites of identity_hash that might revoke biases have  
  71.     // been checked to make sure they can handle a safepoint. The  
  72.     // added check of the bias pattern is to avoid useless calls to  
  73.     // thread-local storage.  
  74.     if (obj->mark()->has_bias_pattern()) {  
  75.       // Box and unbox the raw reference just in case we cause a STW safepoint.  
  76.       Handle hobj (Self, obj) ;  
  77.       // Relaxing assertion for bug 6320749.  
  78.       assert (Universe::verify_in_progress() ||  
  79.               !SafepointSynchronize::is_at_safepoint(),  
  80.              "biases should not be seen by VM thread here");  
  81.       BiasedLocking::revoke_and_rebias(hobj, false, JavaThread::current());  
  82.       obj = hobj() ;  
  83.       assert(!obj->mark()->has_bias_pattern(), "biases should be revoked by now");  
  84.     }  
  85.   }  
  86.   
  87.   // hashCode() is a heap mutator ...  
  88.   // Relaxing assertion for bug 6320749.  
  89.   assert (Universe::verify_in_progress() ||  
  90.           !SafepointSynchronize::is_at_safepoint(), "invariant") ;  
  91.   assert (Universe::verify_in_progress() ||  
  92.           Self->is_Java_thread() , "invariant") ;  
  93.   assert (Universe::verify_in_progress() ||  
  94.          ((JavaThread *)Self)->thread_state() != _thread_blocked, "invariant") ;  
  95.   
  96.   ObjectMonitor* monitor = NULL;  
  97.   markOop temp, test;  
  98.   intptr_t hash;  
  99.   markOop mark = ReadStableMark (obj);  
  100.   
  101.   // object should remain ineligible for biased locking  
  102.   assert (!mark->has_bias_pattern(), "invariant") ;  
  103.   
  104.   if (mark->is_neutral()) {  
  105.     hash = mark->hash();              // this is a normal header  
  106.     if (hash) {                       // if it has hash, just return it  
  107.       return hash;  
  108.     }  
  109.     hash = get_next_hash(Self, obj);  // allocate a new hash code  
  110.     temp = mark->copy_set_hash(hash); // merge the hash code into header  
  111.     // use (machine word version) atomic operation to install the hash  
  112.     test = (markOop) Atomic::cmpxchg_ptr(temp, obj->mark_addr(), mark);  
  113.     if (test == mark) {  
  114.       return hash;  
  115.     }  
  116.     // If atomic operation failed, we must inflate the header  
  117.     // into heavy weight monitor. We could add more code here  
  118.     // for fast path, but it does not worth the complexity.  
  119.   } else if (mark->has_monitor()) {  
  120.     monitor = mark->monitor();  
  121.     temp = monitor->header();  
  122.     assert (temp->is_neutral(), "invariant") ;  
  123.     hash = temp->hash();  
  124.     if (hash) {  
  125.       return hash;  
  126.     }  
  127.     // Skip to the following code to reduce code size  
  128.   } else if (Self->is_lock_owned((address)mark->locker())) {  
  129.     temp = mark->displaced_mark_helper(); // this is a lightweight monitor owned  
  130.     assert (temp->is_neutral(), "invariant") ;  
  131.     hash = temp->hash();              // by current thread, check if the displaced  
  132.     if (hash) {                       // header contains hash code  
  133.       return hash;  
  134.     }  
  135.     // WARNING:  
  136.     //   The displaced header is strictly immutable.  
  137.     // It can NOT be changed in ANY cases. So we have  
  138.     // to inflate the header into heavyweight monitor  
  139.     // even the current thread owns the lock. The reason  
  140.     // is the BasicLock (stack slot) will be asynchronously  
  141.     // read by other threads during the inflate() function.  
  142.     // Any change to stack may not propagate to other threads  
  143.     // correctly.  
  144.   }  
  145.   
  146.   // Inflate the monitor to set hash code  
  147.   monitor = ObjectSynchronizer::inflate(Self, obj);  
  148.   // Load displaced header and check it has hash code  
  149.   mark = monitor->header();  
  150.   assert (mark->is_neutral(), "invariant") ;  
  151.   hash = mark->hash();  
  152.   if (hash == 0) {  
  153.     hash = get_next_hash(Self, obj);  
  154.     temp = mark->copy_set_hash(hash); // merge hash code into header  
  155.     assert (temp->is_neutral(), "invariant") ;  
  156.     test = (markOop) Atomic::cmpxchg_ptr(temp, monitor, mark);  
  157.     if (test != mark) {  
  158.       // The only update to the header in the monitor (outside GC)  
  159.       // is install the hash code. If someone add new usage of  
  160.       // displaced header, please update this code  
  161.       hash = test->hash();  
  162.       assert (test->is_neutral(), "invariant") ;  
  163.       assert (hash != 0, "Trivial unexpected object/monitor header usage.");  
  164.     }  
  165.   }  
  166.   // We finally get the hash  ,看到这句话,就特别兴奋,WE FINALLY GET THE HASH!!!!  
  167.   return hash;  
  168. }  

    

        源码来源: minglisoft.cn/technology

0 0
原创粉丝点击