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()方法具体是怎么实现的?的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 免费的录屏软件,来试试这一款软件吧!
- 下一篇: 学C#的感想