前言:在工做中使用==埋下的坑這篇博文的最後,我想到了兩個問題,其中一個是——爲何 int int1=99;int int2=99;int1和int2的identityHashCode是同樣的哪?爲何float float1=99;float float2=99;float1和float2的identityHashCode是不同的哪?那就須要瞭解identityHashCode的生成規則了,須要瞭解一下java的內存地址分配規則了。html
今天的事情很少,我就查了查資料,找到了對應的底層實現的方式,而且也驗證了hashCode和identityHashCode 的關係這篇博文中的部分觀點。java
本文將根據openJDK 6源碼,向你展現Java語言中的Object對象的hashCode() 方法和System對象的identityHashCode()方法的底層實現的神祕面紗,我將一步一步地向你介紹Java Object對象的hashCode() 方法和System對象的identityHashCode()方法到底底層調用了什麼函數。爲了更好地瞭解這個過程,你能夠本身下載openJDK 6 源碼,親自查看和跟蹤源碼,瞭解Object對象的hashCode() 方法和System對象的identityHashCode()方法的生成過程:c++
openJDK 6 下載地址:http://download.java.net/openjdk/jdk6/算法
1:java.lang.System.java類的identityHashCode()方法以下所示——在 openjdk-6-src-b27-26_oct_2012\jdk\src\share\classes\java\lang 目錄下,它是一個靜態的本地方法安全
/** * Returns the same hash code for the given object as * would be returned by the default method hashCode(), * whether or not the given object's class overrides * hashCode(). * The hash code for the null reference is zero. * * @param x object for which the hashCode is to be calculated * @return the hashCode * @since JDK1.1 */ public static native int identityHashCode(Object x);
2:java.lang.System.java類的identityHashCode()方法的本地c語言的實現——System.c——在 openjdk-6-src-b27-26_oct_2012\jdk\src\share\native\java\lang 目錄下,他調用的是JVM_IHashCode()方法app
JNIEXPORT jint JNICALL Java_java_lang_System_identityHashCode(JNIEnv *env, jobject this, jobject x) { return JVM_IHashCode(env, x); }
3:JVM_IHashCode()方法在 openjdk-6-src-b27-26_oct_2012\hotspot\src\share\vm\prims\jvm.cpp文件中,它又調用ObjectSynchronizer::FastHashCode()方法less
// java.lang.Object /////////////////////////////////////////////// JVM_ENTRY(jint, JVM_IHashCode(JNIEnv* env, jobject handle)) JVMWrapper("JVM_IHashCode"); // as implemented in the classic virtual machine; return 0 if object is NULL return handle == NULL ? 0 : ObjectSynchronizer::FastHashCode (THREAD, JNIHandles::resolve_non_null(handle)) ; JVM_END
4:ObjectSynchronizer::FastHashCode()方法在 openjdk-6-src-b27-26_oct_2012\hotspot\src\share\vm\runtime\synchronizer.cpp文件中,它是最終實現hashCode()和identityHashCode()方法的方法,核心的實現代碼以下,咱們從這裏也能夠看獲得,仍是比較複雜的,並非簡單取一個對象的引用地址那麼簡單。jvm
// 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. 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()); obj = hobj() ; assert(!obj->mark()->has_bias_pattern(), "biases should be revoked by now"); } } // hashCode() is a heap mutator ... // Relaxing assertion for bug 6320749. 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; markOop mark = ReadStableMark (obj); // object should remain ineligible for biased locking assert (!mark->has_bias_pattern(), "invariant") ; if (mark->is_neutral()) { hash = mark->hash(); // this is a normal header if (hash) { // if it has hash, just return it return hash; } hash = get_next_hash(Self, obj); // allocate a new hash code 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. } else if (mark->has_monitor()) { monitor = mark->monitor(); temp = monitor->header(); assert (temp->is_neutral(), "invariant") ; 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())) { 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; } // 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 mark = monitor->header(); assert (mark->is_neutral(), "invariant") ; hash = mark->hash(); 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 return hash; }
5:java.lang.Object.java類的hashCode()方法以下所示——在 openjdk-6-src-b27-26_oct_2012\jdk\src\share\classes\java\lang 目錄下,它是一個本地方法async
/** * Returns a hash code value for the object. This method is * supported for the benefit of hashtables such as those provided by * <code>java.util.Hashtable</code>. * <p> * The general contract of <code>hashCode</code> is: * <ul> * <li>Whenever it is invoked on the same object more than once during * an execution of a Java application, the <tt>hashCode</tt> method * must consistently return the same integer, provided no information * used in <tt>equals</tt> comparisons on the object is modified. * This integer need not remain consistent from one execution of an * application to another execution of the same application. * <li>If two objects are equal according to the <tt>equals(Object)</tt> * method, then calling the <code>hashCode</code> method on each of * the two objects must produce the same integer result. * <li>It is <em>not</em> required that if two objects are unequal * according to the {@link java.lang.Object#equals(java.lang.Object)} * method, then calling the <tt>hashCode</tt> method on each of the * two objects must produce distinct integer results. However, the * programmer should be aware that producing distinct integer results * for unequal objects may improve the performance of hashtables. * </ul> * <p> * As much as is reasonably practical, the hashCode method defined by * class <tt>Object</tt> does return distinct integers for distinct * objects. (This is typically implemented by converting the internal * address of the object into an integer, but this implementation * technique is not required by the * Java<font size="-2"><sup>TM</sup></font> programming language.) * * @return a hash code value for this object. * @see java.lang.Object#equals(java.lang.Object) * @see java.util.Hashtable */ public native int hashCode();
6:java.lang.Object.java類的hashCode()方法的本地c語言的實現——Object.c——在 openjdk-6-src-b27-26_oct_2012\jdk\src\share\native\java\lang 目錄下,他調用的也是JVM_IHashCode()方法,因而可知咱們在hashCode和identityHashCode 的關係中的觀點,在此處也獲得了再次的驗證ide
static JNINativeMethod methods[] = { {"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}, };
7:如上所示,通過一步步的分析,咱們已經瞭解到了 hashCode和identityHashCode底層究竟是怎麼生成的,不過有些事情這裏要在下面補充一下。
7-1:本地方法是什麼東西?
本地方法是指用本地程序設計語言,好比:c或者c++,來編寫的特殊方法。在java語言中經過native關鍵字來修飾,經過Java Native Interface(JNI)技術來支持java應用程序來調用本地方法。
7-2:本地方法的特色是什麼?
本地方法在本地語言中能夠執行任意的計算任務,並返回到java程序設計語言中。
7-3:本地方法的用途是有哪些?
從歷史上看本地方法主要有三種用途。
1)提供「訪問特定於平臺的機制」的能力,好比:訪問註冊表和文件鎖。
2)提供訪問遺留代碼庫的能力,從而能夠訪問遺留數據。
3)能夠經過本地語言,編寫應用程序中注重性能的部分,以提升系統的性能。
7-4:使用本地方法的優缺點是什麼?
整體來說使用本地方須要格外的謹慎,由於本地代碼中的一個bug就有可能破壞掉整個應用程序。
使用本地代碼的優勢是:提升系統性能,訪問特定於平臺的機制。
使用本地代碼的缺點是:
1)由於本地語言是不安全的,因此,使用本地方法的應用程序也不能免受內存毀壞錯誤的影響。
2)由於本地語言是與平臺相關的,使用本地方法的應用程序也再也不是可自由移植的。
3)使用本地方法的應用程序更難調試
4)在進入和退出本地代碼時,須要相關的固定開銷,因此,若是本地代碼時作的少許的工做,本地方法就可能下降性能。
5)須要「膠合代碼」的本地方法編寫起來的單調乏味,而且難以閱讀。
8:參考