1.modCount是什么?A8站源码交易平台
相信很多同学都会在List或hashMap近亲系列源码中都会看到这个modCount变量,简言之,从字面意思了解modCount,批改的次数。A8站源码交易平台
2.modCount的作用
通常地,在集结源码中存在这个modCount变量时,基本上可以阐明这个类是线程不安全的。这个变量在集结初始化的时分,就将modCount赋值给exceptCound;在集结迭代器中,对modCount与exceptCount进行比较,假设出现两者不相等,阐明有其他线程对该集结进行了批改操作(add或remove),这个时分体系就会抛出CurrentModifactionException失常,这从逻辑上避免了多线程并发操作集结时,迭代器中数据紊乱的局势。
3.HashMap源码视点剖析modCount
3.1 map进行put的时分,modCount++
/**
* Associates the specified value with the specified key in this map.
* If the map previously contained a mapping for the key, the old
* value is replaced.
*
* @param key key with which the specified value is to be associated
* @param value value to be associated with the specified key
* @return the previous value associated withkey, or
* nullif there was no mapping forkey.
* (Anullreturn can also indicate that the map
* previously associatednullwithkey.)
*/
public V put(K key, V value) {
return putVal(hash(key), key, value, false, true);
}
/**
* Implements Map.put and related methods.
*
* @param hash hash for key
* @param key the key
* @param value the value to put
* @param onlyIfAbsent if true, don't change existing value
* @param evict if false, the table is in creation mode.
* @return previous value, or null if none
*/
final V putVal(int hash, K key, V value, boolean onlyIfAbsent,
boolean evict) {
Node
if ((tab = table) == null || (n = tab.length) == 0)
n = (tab = resize()).length;
if ((p = tab[i = (n - 1) & hash]) == null)
tab[i] = newNode(hash, key, value, null);
else {
Node
if (p.hash == hash &&
((k = p.key) == key || (key != null && key.equals(k))))
e = p;
else if (p instanceof TreeNode)
e = ((TreeNode
else {
for (int binCount = 0; ; ++binCount) {
if ((e = p.next) == null) {
p.next = newNode(hash, key, value, null);
if (binCount >= TREEIFY_THRESHOLD - 1) // -1 for 1st
treeifyBin(tab, hash);
break;
}
if (e.hash == hash &&
((k = e.key) == key || (key != null && key.equals(k))))
break;
p = e;
}
}
if (e != null) { // existing mapping for key
V oldValue = e.value;
if (!onlyIfAbsent || oldValue == null)
e.value = value;
afterNodeAccess(e);
return oldValue;
}
}
++modCount;
if (++size > threshold)
resize();
afterNodeInsertion(evict);
return null;
}
A8站源码交易平台
3.2 map删去key的时分,modCount++
/**
* Removes the mapping for the specified key from this map if present.
*
* @param key key whose mapping is to be removed from the map
* @return the previous value associated withkey, or
* nullif there was no mapping forkey.
* (Anullreturn can also indicate that the map
* previously associatednullwithkey.)
*/
public V remove(Object key) {
Node
return (e = removeNode(hash(key), key, null, false, true)) == null ?
null : e.value;
}
/**
* Implements Map.remove and related methods.
*
* @param hash hash for key
* @param key the key
* @param value the value to match if matchValue, else ignored
* @param matchValue if true only remove if value is equal
* @param movable if false do not move other nodes while removing
* @return the node, or null if none
*/
final Node
boolean matchValue, boolean movable) {
Node
if ((tab = table) != null && (n = tab.length) > 0 &&
(p = tab[index = (n - 1) & hash]) != null) {
Node
if (p.hash == hash &&
((k = p.key) == key || (key != null && key.equals(k))))
node = p;
else if ((e = p.next) != null) {
if (p instanceof TreeNode)
node = ((TreeNode
else {
do {
if (e.hash == hash &&
((k = e.key) == key ||
(key != null && key.equals(k)))) {
node = e;
break;
}
p = e;
} while ((e = e.next) != null);
}
}
if (node != null && (!matchValue || (v = node.value) == value ||
(value != null && value.equals(v)))) {
if (node instanceof TreeNode)
((TreeNode
else if (node == p)
tab[index] = node.next;
else
p.next = node.next;
++modCount;
--size;
afterNodeRemoval(node);
return node;
}
}
return null;
}
A8站源码交易平台
3.3 map进行迭代,modCount与expertCount比较
/**
* Returns a {@link Set} view of the keys contained in this map.
* The set is backed by the map, so changes to the map are
* reflected in the set, and vice-versa. If the map is modified
* while an iteration over the set is in progress (except through
* the iterator's ownremoveoperation), the results of
* the iteration are undefined. The set supports element removal,
* which removes the corresponding mapping from the map, via the
*Iterator.remove,Set.remove,
*removeAll,retainAll, andclear
* operations. It does not support theaddoraddAll
* operations.
*
* @return a set view of the keys contained in this map
*/
public SetkeySet() {
Setks = keySet;
if (ks == null) {
ks = new KeySet();
keySet = ks;
}
return ks;
}
final class KeySet extends AbstractSet{
public final int size() { return size; }
public final void clear() { HashMap.this.clear(); }
public final Iteratoriterator() { return new KeyIterator(); }
public final boolean contains(Object o) { return containsKey(o); }
public final boolean remove(Object key) {
return removeNode(hash(key), key, null, false, true) != null;
}
public final Spliteratorspliterator() {
return new KeySpliterator<>(HashMap.this, 0, -1, 0, 0);
}
public final void forEach(Consumer action) {
Node
if (action == null)
throw new NullPointerException();
if (size > 0 && (tab = table) != null) {
int mc = modCount;
for (int i = 0; i < tab.length; ++i) {
for (Node
action.accept(e.key);
}
if (modCount != mc)
throw new ConcurrentModificationException();
}
}
}
A8站源码交易平台