您的位置:首页 > 其它

hashmap造成死循环

2016-04-16 16:27 274 查看
转载地址:/article/6109929.html

注:

public V put(K key, V value)
{
......
//算Hash值
int hash = hash(key.hashCode()); //会出现相同hash值
int i = indexFor(hash, table.length);
//如果该key已被插入,则替换掉旧的value (链接操作)
for (Entry<K,V> e = table[i]; e != null; e = e.next) {
Object k;
//key.equals(k)针对字符串,这样s1="abc"和s2=new String("abc")key相同
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;
}


这是transfer方法里面的一段代码

do {
Entry<K,V> next = e.next; // <--假设线程一执行到这里就被调度挂起了
int i = indexFor(e.hash, newCapacity);
//指向第一个newTable[i]中的第一个Entry
e.next = newTable[i];
//自己为newTable[i]中的第一个Entry
newTable[i] = e;
e = next;
} while (e != null);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: