System.identityHashCode和hashCode的區(qū)別及說明
System.identityHashCode和hashCode的區(qū)別
測(cè)試:
public class HashCodeDemo { public static void main(String[] args) { String str = new String("test"); String str2= new String("test"); System.out.println(str.hashCode()); System.out.println(str2.hashCode()); System.out.println(System.identityHashCode(str)); System.out.println(System.identityHashCode(str2)); System.out.println("----------------test------------"); Test test = new Test(); Test test2 = new Test(); System.out.println(test.hashCode()); System.out.println(test2.hashCode()); System.out.println(System.identityHashCode(test)); System.out.println(System.identityHashCode(test2)); } }
運(yùn)行結(jié)果:
為什么String調(diào)用hashCode()和System.identityHashCode()返回值不同呢?為什么Test調(diào)用hashCode()和System.identityHashCode()返回值又相同呢?
System.identityHashCode()和hashCode()到底有什么不同呢?這里個(gè)人簡(jiǎn)單分析一下,有不足之處勞煩指出。
System.identityHashCode底層實(shí)現(xiàn)
System.identityHashCode底層調(diào)用C語(yǔ)言System.c來實(shí)現(xiàn)
openjdk源碼路徑:jdk-935758609767\src\share\native\java\lang\System.c
// 核心代碼: Java_java_lang_System_identityHashCode(JNIEnv *env, jobject this, jobject x) { return JVM_IHashCode(env, x); }
其中調(diào)用jvm.cpp的JVM_IHashCode()方法
hotspot源碼路徑:hotspot-37240c1019fd\src\share\vm\prims\jvm.cpp
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
再來看一下synchronizer.cpp的ObjectSynchronizer::FastHashCode()方法
hotspot源碼路徑:hotspot-37240c1019fd\src\share\vm\runtime\synchronizer.cpp
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; }
其中,調(diào)用的核心方法是get_next_hash()
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 ; // for sensitivity testing } else if (hashCode == 3) { value = ++GVars.hcSequence ; } 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. 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 ; } value &= markOopDesc::hash_mask; if (value == 0) value = 0xBAD ; assert (value != markOopDesc::no_hash, "invariant") ; TEVENT (hashCode: GENERATE) ; return value; }
根據(jù)hashcode值,可以分為以下方法:
0- 隨機(jī)生成值
1- 獲取對(duì)象的實(shí)際內(nèi)存地址
2- 返回1,用于靈敏度測(cè)試
3- 自增 4- 第二種的變種
5- xorshift算法,有興趣可以看一下https://en.wikipedia.org/wiki/Xorshift
jdk1.8前,默認(rèn)hashcode為0,可通過globals.hpp文件查看,調(diào)用第一個(gè)方法,隨機(jī)生成hashcode
globals.hpp源碼路徑:hotspot\src\share\vm\runtime\globals.hpp
product(intx, hashCode, 0,“(Unstable) select hashCode generation algorithm”)
jdk1.8后,默認(rèn)為5,使用xorshift 算法生成hashcode
product(intx, hashCode, 5,“(Unstable) select hashCode generation algorithm”)
同時(shí)可以通過-XX:hashCode=N來修改jvm默認(rèn)值來修改調(diào)用方法
查看jvm默認(rèn)值java -XX:+PrintFlagsFinal -version
Object.hashCode底層實(shí)現(xiàn)
通過查看openjdk源碼Object.c
Object.c源碼路徑:jdk-935758609767\src\share\native\java\lang\Object.c
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}, };
發(fā)現(xiàn)還是和System.identityHashCode一樣都是調(diào)用JVM_IHashCode方法。
總結(jié)
- 當(dāng)hashCode()未被重寫時(shí),System.identityHashCode()和hashCode()返回值相同,都是調(diào)用底層JVM_IHashCode方法
- 當(dāng)hashCode()被重寫,則System.identityHashCode()和hashCode()返回值不同。hashCode()返回重寫結(jié)果,System.identityHashCode()返回底層生成hashcode
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Spring Boot命令行運(yùn)行器的實(shí)現(xiàn)方法
這篇文章主要介紹了Spring Boot命令行運(yùn)行器的實(shí)現(xiàn)方法,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-10-10mybatis中string和date的轉(zhuǎn)換方式
這篇文章主要介紹了mybatis中string和date的轉(zhuǎn)換方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08java啟動(dòng)jar包修改JVM默認(rèn)內(nèi)存問題
這篇文章主要介紹了java啟動(dòng)jar包修改JVM默認(rèn)內(nèi)存問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-02-02詳解java一維數(shù)組及練習(xí)題實(shí)例
在本篇文章里小編給大家整理了關(guān)于java一維數(shù)組及練習(xí)題的相關(guān)知識(shí)點(diǎn)和實(shí)例代碼,有需要的朋友們跟著學(xué)習(xí)下。2019-07-07SpringBoot的WebSocket實(shí)現(xiàn)單聊群聊
這篇文章主要為大家詳細(xì)介紹了SpringBoot的WebSocket實(shí)現(xiàn)單聊群聊,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-02-02基于JavaMail的Java實(shí)現(xiàn)簡(jiǎn)單郵件發(fā)送功能
這篇文章主要為大家詳細(xì)介紹了基于JavaMail的Java實(shí)現(xiàn)簡(jiǎn)單郵件發(fā)送功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-09-09通過實(shí)例解析POJO和JavaBean的區(qū)別
這篇文章主要介紹了通過實(shí)例解析POJO和JavaBean的區(qū)別,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-07-07SpringBoot集成Sharding-JDBC實(shí)現(xiàn)分庫(kù)分表方式
這篇文章主要介紹了SpringBoot集成Sharding-JDBC實(shí)現(xiàn)分庫(kù)分表方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-07-07SpringBoot+WebSocket實(shí)現(xiàn)即時(shí)通訊功能(Spring方式)
今天給大家分享一個(gè)SpringBoot+WebSocket實(shí)現(xiàn)即時(shí)通訊功能(Spring方式),WebSocket是一種在單個(gè)TCP連接上進(jìn)行全雙工通信的協(xié)議,文章通過代碼示例給大家介紹的非常詳細(xì),需要的朋友可以參考下2023-10-10