[Java基礎要義] Java語言中Object對象的hashCode()取值的底層算法是怎樣實現的?

 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方法指針來處理:

  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中定義,如下:

  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 文件中實現,其核心代碼實現如下所示:

  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

發佈了39 篇原創文章 · 獲贊 0 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章