日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

hashcode java_java 的Object类的hashcode()方法具体是怎么实现的?

發布時間:2023/12/9 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 hashcode java_java 的Object类的hashcode()方法具体是怎么实现的? 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

輕松解說Object.hashcode()的實現,讓你看著不累!

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.

// 將obj對象包裝成一個句柄->hobj

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());

// 這里根據Handle類定義的無參函數對象,將obj再取出來

obj = hobj() ;

// 看看是不是確保成功撤銷了

assert(!obj->mark()->has_bias_pattern(), "biases should be revoked by now");

}

}

// hashCode() is a heap mutator ...

// Relaxing assertion for bug 6320749.

/**

* 1. 確保當前的執行路徑不處在全局安全點上;

* 2. 確保當前線程是個JavaThread

* 3. 確保當前線程沒有被block

*/

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;

// 讀出一個穩定的mark,防止obj正處于鎖膨脹狀態,如果正在膨脹,就等它膨脹完,再讀出來

markOop mark = ReadStableMark (obj);

// object should remain ineligible for biased locking

// 我擦,你不會還處于“已偏向”狀態吧

assert (!mark->has_bias_pattern(), "invariant") ;

// 如果mark現在處于中立狀態了->unlock 這時候mark的結構應該是 [hash|age|0|01]

if (mark->is_neutral()) {

// 看看mark中是不是有一個存在的hash值

hash = mark->hash(); // this is a normal header

// 我靠,有了,省事了,直接返回吧

if (hash) { // if it has hash, just return it

return hash;

}

// 沒有,那我就根據hashCode的算法規則重新算一個出來吧

hash = get_next_hash(Self, obj); // allocate a new hash code

// 把上面的hash結果merge到mark中去,看到我寫的那個結構了嗎,放到hash那個位置

// 得到一個臨時的temp,為什么這么干,繼續看下面

temp = mark->copy_set_hash(hash); // merge the hash code into header

// use (machine word version) atomic operation to install the hash

// 上面的E文注釋寫得已經很直白了,CAS安裝hash值咯

test = (markOop) Atomic::cmpxchg_ptr(temp, obj->mark_addr(), mark);

if (test == mark) {

// 看來CAS操作成功了,返回hash

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.

// 媽的,CAS失敗了,上面E文說了,我們要把對象頭膨脹成重量級鎖了!

// 看看重鎖狀態時,mark的結構吧- monitor_ptr|10

// Anyway, 看看現在你是不是已經是重鎖狀態了吧

} else if (mark->has_monitor()) {

// 好家伙,你已經膨脹成重鎖了嘛

monitor = mark->monitor();

// 那么我們就從ObjectMonitor對象中將mark取出來看看吧

temp = monitor->header();

// 這時候mark應該是無鎖中立狀態了,結構看上面吧!

assert (temp->is_neutral(), "invariant") ;

// 完事OK,取出來返回吧!

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())) {

// 直接從線程棧里把mark取出來吧

temp = mark->displaced_mark_helper(); // this is a lightweight monitor owned

// mark不是中立狀態?你搞笑吧!

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

// 從重鎖對象中load出對象頭mark來,看看是否已經有了一個hash值了

mark = monitor->header();

// check 不解釋了

assert (mark->is_neutral(), "invariant") ;

hash = mark->hash();

// hash值還是空的,well,我們還是算一個出來吧!

// 下面的邏輯跟上面的一段一致,哥就不用費口舌了...

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

// 費了好大勁,終于拿到hash值了,雞凍~

return hash;

}

上面還有一個重要的函數get_next_hash, hachCode是hash的生成策略,默認值是5,可在虛擬機啟動時配置(由于算法比較嚴肅,輕松不了了~~):

// hashCode() generation :

//

// Possibilities:

// * MD5Digest of {obj,stwRandom}

// * CRC32 of {obj,stwRandom} or any linear-feedback shift register function.

// * A DES- or AES-style SBox[] mechanism

// * One of the Phi-based schemes, such as:

// 2654435761 = 2^32 * Phi (golden ratio)

// HashCodeValue = ((uintptr_t(obj) >> 3) * 2654435761) ^ GVars.stwRandom ;

// * A variation of Marsaglia's shift-xor RNG scheme.

// * (obj ^ stwRandom) is appealing, but can result

// in undesirable regularity in the hashCode values of adjacent objects

// (objects allocated back-to-back, in particular). This could potentially

// result in hashtable collisions and reduced hashtable efficiency.

// There are simple ways to "diffuse" the middle address bits over the

// generated hashCode values:

//

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

}

總結

以上是生活随笔為你收集整理的hashcode java_java 的Object类的hashcode()方法具体是怎么实现的?的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 欧美日韩一区二区三区四区 | 久久99精品久久只有精品 | 国产91精| 成 人免费va视频 | 黄色一级大片在线观看 | 91九色高潮 | 国产高清视频在线免费观看 | 污视频免费网站 | 国产传媒在线观看 | 色噜噜在线播放 | 一本久久a精品一合区久久久 | 奴性女会所调教 | 美国av一区二区 | www.天天综合 | 18无码粉嫩小泬无套在线观看 | 秋霞国产午夜精品免费视频 | 国产91丝袜在线播放0 | 色婷婷中文字幕 | 人人爽人人做 | 国产又粗又猛又爽又黄视频 | 亚洲av日韩精品久久久久久久 | 国产精品资源在线观看 | 亚洲在线免费 | 国产午夜精品在线 | 一区二区三区小说 | 中文字幕乱妇无码av在线 | 黄色片网站在线 | 蜜桃臀av | 毛片国产| 亚洲日本在线观看 | 天天插天天操 | 久久久www | 制服丝袜在线看 | 亚洲色图欧美日韩 | 97超级碰碰 | 青青艹av | 国内精品视频在线观看 | 亚洲天堂手机在线 | 亚洲欧美在线免费观看 | 99色99 | 天堂va蜜桃一区二区三区漫画版 | 精品少妇一二三区 | 91在线国产观看 | 91在线视频国产 | 久热超碰 | 中文字幕av一区二区三区人妻少妇 | 麻豆视频在线播放 | 国产精品污| 亚洲午夜久久 | 羞羞的网站在线观看 | 二级毛片在线观看 | 人妻熟女aⅴ一区二区三区汇编 | 成年人免费网站视频 | 在线sese| 欧美成人免费在线观看 | 桃谷绘里香番号 | 国产亚洲综合精品 | 成人免费在线网站 | 精品人伦一区二区三区 | 亚洲aaa视频 | 狠狠操很很干 | av片免费在线 | 欧美s码亚洲码精品m码 | 色网址在线 | 久久精品国产久精国产 | 久久久久久久艹 | 欧美午夜在线观看 | 做视频 | 美女黄色真播 | 1024毛片基地 | 波多野结衣精品视频 | 国产熟妇乱xxxxx大屁股网 | 亚洲欧美日韩精品色xxx | 影音先锋在线视频观看 | 欧美美女性生活视频 | 欧美精品久久久久久久多人混战 | 拔插拔插海外华人永久免费 | 日本黄色片段 | 欧美成人综合在线 | 亚洲AV成人午夜无码精品久久 | 99亚洲国产精品 | av电影中文字幕 | 日本三级午夜理伦三级三 | 亚洲无吗在线观看 | caopeng视频| 中国少妇色| 99久久精品国产一区二区成人 | 亚洲精品国产精品国自产 | 午夜综合网 | 另类中文字幕 | 久久6精品 | 国产91精品ai换脸 | 欧美三级欧美成人高清 | 黄色一区二区三区四区 | 成人黄色a级片 | 欧美日韩成人网 | av小说免费在线观看 | 青草青在线 | 国产精品视频一区二区三区不卡 |