???磺Java HashMap???????
???????????? ???????[ 2013/6/14 10:01:31 ] ????????
???????????????????·???????????CPU????????????????????·???????Σ????????Java?????????????????HashMap???Race Condition?????????????????????????4??5?????????????????????????д??????Java??HashMap?????????????????????±?????????????????????????????????????????£????????“HashMap Infinite Loop”????????????????????£??????????????????????????д?????????????????£?????????????????????“Race Condition”??????γ???
???????????
????????????Java????????Щ????????HashMap??????????????????????????????ж?????????????????????????????????????????????????????????????????????????????????CPU?????????????????Hang????HashMap.get???????????????????????????????????????????????????????????????????????????????????
??????????????????????????????????HashMap???????????????Java??????HashMap????????????????ConcurrentHashMap??
????????????????????????о???????
????Hash???????
?????????????????HashMap???????????????
????HashMap????????????????饗?????table[]????????????е?key???????key??????????????Hash?????key????????????±?i?????????
????????????????table[]?????С?????????2???????????10??keys??????????????????????????O??1?????????????????????????????????O??n????????Hash?????????ο???Hash Collision DoS ????????
?????????Hash????????????????????????????Hash???????????????????????????????????????г????趨??thredhold??????????????????Hash????磬?????????????????Hash?????????????????????顣???rehash?????????????
???????????????????????????????
????HashMap??rehash?????
???????棬???????????Java??HashMap???????
????Put???Key??Value???Hash???У?
public V put(K key?? V value)
????????????????
void addEntry(int hash?? K key?? V value?? int bucketIndex)
?????????????????hash???????????????Hash?????????μ?Hash???С?
void resize(int newCapacity)
{
......
//??Hash?
int hash = hash(key.hashCode());
int i = indexFor(hash?? table.length);
//?????key??????????滻????value ???????????
for (Entry
Object k;
if (e.hash == hash && ((k = e.key) == key || key.equals(k))) {
V oldValue = e.value;
e.value = value;
e.recordAccess(this);
return oldValue;
}
}
modCount++;
//??key????????????????????
addEntry(hash?? key?? value?? i);
return null;
}
{
Entry
table[bucketIndex] = new Entry
//???????size????????????趨?????threshold??????????????resize
if (size++ >= threshold)
resize(2 * table.length);
}
{
Entry[] oldTable = table;
int oldCapacity = oldTable.length;
......
//????????μ?Hash Table
Entry[] newTable = new Entry[newCapacity];
//??Old Hash Table???????????New Hash Table??
transfer(newTable);
table = newTable;
threshold = (int)(newCapacity * loadFactor);
}
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11