Java Object.hashCode()返回的是對象內(nèi)存地址浊猾?

基于OpenJDK 8

一直以為Java Object.hashCode()的結(jié)果就是通過對象的內(nèi)存地址做相關(guān)運算得到的钉汗,但是無意在網(wǎng)上看到有相應(yīng)的意見爭論,故抽時間從源碼層面驗證了剖析了hashCode的默認(rèn)計算方法。

先說結(jié)論:OpenJDK8 默認(rèn)hashCode的計算方法是通過和當(dāng)前線程有關(guān)的一個隨機(jī)數(shù)+三個確定值老充,運用Marsaglia's xorshift scheme隨機(jī)數(shù)算法得到的一個隨機(jī)數(shù)葡盗。和對象內(nèi)存地址無關(guān)。

下面通過查找和分析OpenJDK8源碼實現(xiàn)來一步步分析。

1. 查找java.lang.Object.hashCode()源碼

public native int hashCode();

2. 導(dǎo)出Object的JNI頭文件

切換到Object.class文件所在目錄觅够,執(zhí)行 javah -jni java.lang.Object胶背,得到j(luò)ava_lang_Object.h文件,文件內(nèi)容如下:

/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class java_lang_Object */

#ifndef _Included_java_lang_Object
#define _Included_java_lang_Object
#ifdef __cplusplus
extern "C" {
#endif
/*
 * Class:     java_lang_Object
 * Method:    registerNatives
 * Signature: ()V
 */
JNIEXPORT void JNICALL Java_java_lang_Object_registerNatives
  (JNIEnv *, jclass);

/*
 * Class:     java_lang_Object
 * Method:    getClass
 * Signature: ()Ljava/lang/Class;
 */
JNIEXPORT jclass JNICALL Java_java_lang_Object_getClass
  (JNIEnv *, jobject);

/*
 * Class:     java_lang_Object
 * Method:    hashCode
 * Signature: ()I
 */
JNIEXPORT jint JNICALL Java_java_lang_Object_hashCode
  (JNIEnv *, jobject);

/*
 * Class:     java_lang_Object
 * Method:    clone
 * Signature: ()Ljava/lang/Object;
 */
JNIEXPORT jobject JNICALL Java_java_lang_Object_clone
  (JNIEnv *, jobject);

/*
 * Class:     java_lang_Object
 * Method:    notify
 * Signature: ()V
 */
JNIEXPORT void JNICALL Java_java_lang_Object_notify
  (JNIEnv *, jobject);

/*
 * Class:     java_lang_Object
 * Method:    notifyAll
 * Signature: ()V
 */
JNIEXPORT void JNICALL Java_java_lang_Object_notifyAll
  (JNIEnv *, jobject);

/*
 * Class:     java_lang_Object
 * Method:    wait
 * Signature: (J)V
 */
JNIEXPORT void JNICALL Java_java_lang_Object_wait
  (JNIEnv *, jobject, jlong);

#ifdef __cplusplus
}
#endif
#endif

3 . 查看Object的native方法實現(xiàn)

OpenJDK源碼鏈接:http://hg.openjdk.java.net/jdk8u/jdk8u/jdk/file/3462d04401ba/src/share/native/java/lang/Object.c 喘先,查看Object.c文件钳吟,可以看到hashCode()的方法被注冊成由JVM_IHashCode方法指針來處理。

static JNINativeMethod methods[] = {  
    {"hashCode",    "()I",                    (void *)&JVM_IHashCode},//hashcode的方法指針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_IHashCode方法指針在 openjdk\hotspot\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 

從而得知窘拯,真正計算獲得hashCode的值是ObjectSynchronizer::FastHashCode

4 . ObjectSynchronizer::fashHashCode方法的實現(xiàn)

openjdk\hotspot\src\share\vm\runtime\synchronizer.cpp 找到其實現(xiàn)方法红且。

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;
}

該方法中

// 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);
...
}

對hash值真正進(jìn)行了計算,查看get_next_hash方法源碼http://hg.openjdk.java.net/jdk8u/jdk8u/hotspot/file/87ee5ee27509/src/share/vm/runtime/synchronizer.cpp#l555

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;
}

對于OpenJDK8版本涤姊,其默認(rèn)配置http://hg.openjdk.java.net/jdk8u/jdk8u/hotspot/file/87ee5ee27509/src/share/vm/runtime/globals.hpp#l1127 為:

 product(intx, hashCode, 5,                                                \
          "(Unstable) select hashCode generation algorithm")                \

其對應(yīng)的hashCode計算方案為:

    // 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 ;

其中Thread->_hashStateX, Thread->_hashStateY, Thread->_hashStateZ, Thread->_hashStateW在http://hg.openjdk.java.net/jdk8u/jdk8u/hotspot/file/87ee5ee27509/src/share/vm/runtime/thread.cpp#I263 有定義:

   // thread-specific hashCode stream generator state - Marsaglia shift-xor form
  _hashStateX = os::random() ;
  _hashStateY = 842502087 ;
  _hashStateZ = 0x8767 ;    // (int)(3579807591LL & 0xffff) ;
  _hashStateW = 273326509 ;

所以暇番,JDK8 的默認(rèn)hashCode的計算方法是通過和當(dāng)前線程有關(guān)的一個隨機(jī)數(shù)+三個確定值,運用Marsaglia's xorshift scheme隨機(jī)數(shù)算法得到的一個隨機(jī)數(shù)思喊。對xorshift算法有興趣可以參考原論文:https://www.jstatsoft.org/article/view/v008i14/xorshift.pdf 壁酬。
xorshift是由George Marsaglia發(fā)現(xiàn)的一類偽隨機(jī)數(shù)生成器,其通過移位和與或計算恨课,能夠在計算機(jī)上以極快的速度生成偽隨機(jī)數(shù)序列舆乔。其算法的基本實現(xiàn)如下:

unsigned long xor128(){
static unsigned long x=123456789,y=362436069,z=521288629,w=88675123;
unsigned long t;
t=(x?(x<<11));x=y;y=z;z=w; return( w=(w?(w>>19))?(t?(t>>8)) );

這就和上面計算hashCode的OpenJDK代碼對應(yīng)了起來。

5 . 其他幾類hashCode計算方案:

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() ;
  }
  • hashCode == 1
    此類方案將對象的內(nèi)存地址希俩,做移位運算后與一個隨機(jī)數(shù)進(jìn)行異或得到結(jié)果
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 ;
  }
  • hashCode == 2
    此類方案返回固定的1
if (hashCode == 2) {
     value = 1 ;            // for sensitivity testing
  } 
  • hashCode == 3
    此類方案返回一個自增序列的當(dāng)前值
if (hashCode == 3) {
     value = ++GVars.hcSequence ;
  } 
  • hashCode == 4
    此類方案返回當(dāng)前對象的內(nèi)存地址
if (hashCode == 4) {
     value = cast_from_oop<intptr_t>(obj) ;
  }

可以通過在JVM啟動參數(shù)中添加-XX:hashCode=4,改變默認(rèn)的hashCode計算方式诬留。

參考資料:
https://srvaroa.github.io/jvm/java/openjdk/biased-locking/2017/01/30/hashCode.html
https://en.wikipedia.org/wiki/Xorshift
http://www.cnblogs.com/mengyou0304/p/4763220.html
http://stackoverflow.com/questions/2427631/how-is-hashcode-calculated-in-java
http://hllvm.group.iteye.com/group/topic/39183

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末斜纪,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子文兑,更是在濱河造成了極大的恐慌盒刚,老刑警劉巖,帶你破解...
    沈念sama閱讀 216,372評論 6 498
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件绿贞,死亡現(xiàn)場離奇詭異因块,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)籍铁,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,368評論 3 392
  • 文/潘曉璐 我一進(jìn)店門涡上,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人拒名,你說我怎么就攤上這事吩愧。” “怎么了增显?”我有些...
    開封第一講書人閱讀 162,415評論 0 353
  • 文/不壞的土叔 我叫張陵雁佳,是天一觀的道長。 經(jīng)常有香客問我,道長糖权,這世上最難降的妖魔是什么堵腹? 我笑而不...
    開封第一講書人閱讀 58,157評論 1 292
  • 正文 為了忘掉前任,我火速辦了婚禮星澳,結(jié)果婚禮上疚顷,老公的妹妹穿的比我還像新娘。我一直安慰自己禁偎,他們只是感情好腿堤,可當(dāng)我...
    茶點故事閱讀 67,171評論 6 388
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著届垫,像睡著了一般释液。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上装处,一...
    開封第一講書人閱讀 51,125評論 1 297
  • 那天误债,我揣著相機(jī)與錄音,去河邊找鬼妄迁。 笑死寝蹈,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的登淘。 我是一名探鬼主播箫老,決...
    沈念sama閱讀 40,028評論 3 417
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼黔州!你這毒婦竟也來了耍鬓?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 38,887評論 0 274
  • 序言:老撾萬榮一對情侶失蹤流妻,失蹤者是張志新(化名)和其女友劉穎牲蜀,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體绅这,經(jīng)...
    沈念sama閱讀 45,310評論 1 310
  • 正文 獨居荒郊野嶺守林人離奇死亡涣达,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,533評論 2 332
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了证薇。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片度苔。...
    茶點故事閱讀 39,690評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖浑度,靈堂內(nèi)的尸體忽然破棺而出寇窑,到底是詐尸還是另有隱情,我是刑警寧澤箩张,帶...
    沈念sama閱讀 35,411評論 5 343
  • 正文 年R本政府宣布甩骏,位于F島的核電站完残,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏横漏。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,004評論 3 325
  • 文/蒙蒙 一熟掂、第九天 我趴在偏房一處隱蔽的房頂上張望缎浇。 院中可真熱鬧,春花似錦赴肚、人聲如沸素跺。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,659評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽指厌。三九已至,卻和暖如春踊跟,著一層夾襖步出監(jiān)牢的瞬間踩验,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,812評論 1 268
  • 我被黑心中介騙來泰國打工商玫, 沒想到剛下飛機(jī)就差點兒被人妖公主榨干…… 1. 我叫王不留箕憾,地道東北人。 一個月前我還...
    沈念sama閱讀 47,693評論 2 368
  • 正文 我出身青樓拳昌,卻偏偏與公主長得像袭异,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子炬藤,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,577評論 2 353

推薦閱讀更多精彩內(nèi)容