???纾篔ava 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)
{
......
//??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;
}
????????????????
void addEntry(int hash?? K key?? V value?? int bucketIndex)
{
Entry
table[bucketIndex] = new Entry
//???????size????????????瓒�?????threshold??????????????resize
if (size++ >= threshold)
resize(2 * table.length);
}
?????????????????hash???????????????Hash?????????渭?Hash???小?
void resize(int newCapacity)
{
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