HashMap中ConcurrentModificationException異常解讀
HashMap中ConcurrentModificationException異常解讀
HashMap是一個執行緒不安全的集合,如果在遍歷的過程中同時對該集合進行修改操作,例如put,add,remove等, 會丟擲java.util.ConcurrentModificationException異常,那麼究竟這個異常為何丟擲,下面從原始碼層面來分析一下。
跟蹤程式碼:
檢視HashMap原始碼,具體拋該異常的地方為:
final Node<K,V> nextNode() { Node<K,V>[] t; Node<K,V> e = next; if (modCount != expectedModCount) throw new ConcurrentModificationException(); if (e == null) throw new NoSuchElementException(); if ((next = (current = e).next) == null && (t = table) != null) { do {} while (index < t.length && (next = t[index++]) == null); } return e; }
如果HashMap中modCount和expectedModCount不相等,則會丟擲異常
檢視modCount:
具體用途是記錄該HashMap修改次數,比如在對一個HashMap put操作時,會對modCount進行++modCount操作(紅色標註的地方)
final V putVal(int hash, K key, V value, boolean onlyIfAbsent, boolean evict) { Node<K,V>[] tab; Node<K,V> p; int n, i; 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<K,V> e; K k; if (p.hash == hash && ((k = p.key) == key || (key != null && key.equals(k)))) e = p; else if (p instanceof TreeNode) e = ((TreeNode<K,V>)p).putTreeVal(this, tab, hash, key, value); 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; }
而在remove操作的時候,也會對modCount進行同樣的操作:
final Node<K,V> removeNode(int hash, Object key, Object value, boolean matchValue, boolean movable) { Node<K,V>[] tab; Node<K,V> p; int n, index; if ((tab = table) != null && (n = tab.length) > 0 && (p = tab[index = (n - 1) & hash]) != null) { Node<K,V> node = null, e; K k; V v; 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<K,V>)p).getTreeNode(hash, key); 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<K,V>)node).removeTreeNode(this, tab, movable); else if (node == p) tab[index] = node.next; else p.next = node.next; ++modCount; --size; afterNodeRemoval(node); return node; } } return null; }
檢視expectedModCount:
它是HashIterator中的一個變數,在對HashMap迭代的時候,將modCount賦給expectedModCount,具體程式碼:
HashIterator() { expectedModCount = modCount; Node<K,V>[] t = table; current = next = null; index = 0; if (t != null && size > 0) { // advance to first entry do {} while (index < t.length && (next = t[index++]) == null); } }
何時呼叫HashIterator():
檢視HashMap entrySet()原始碼:
public Set<Map.Entry<K,V>> entrySet() { Set<Map.Entry<K,V>> es; return (es = entrySet) == null ? (entrySet = new EntrySet()) : es; }
此處新建一個EntrySet物件,而在對EntrySet進行迭代的時候,會呼叫:
public final Iterator<Map.Entry<K,V>> iterator() { return new EntryIterator(); }
新建一個EntryIterator物件,檢視該類描述:
final class EntryIterator extends HashIterator implements Iterator<Map.Entry<K,V>> { public final Map.Entry<K,V> next() { return nextNode(); } }
它繼承HashIterator,因此在new EntryIterator()的時候會預設呼叫它父類HashIterator的無參構造方法。
總結:
HashMap迭代遍歷的時候,會初始化expectedModCount=modCount,這時候對HashMap進行修改操作,modCount會+1,繼續遍歷的時候expectedModCount!=modCount,繼而丟擲java.util.ConcurrentModificationException異常。
相關文章
- 併發修改異常 ConcurrentModificationException詳解Exception
- ConcurrentModificationException併發修改異常Exception
- bag系列ConcurrentModificationException---併發修改異常Exception
- Laravel核心解讀–異常處理Laravel
- Java-HashMap中put原始碼解讀JavaHashMap原始碼
- HashMap原始碼解讀HashMap原始碼
- 多執行緒下使用List中的subList和remove方法產生的 java.util.ConcurrentModificationException 異常執行緒REMJavaException
- 解讀 K8s Pod 的 13 種異常K8S
- HashMap原始碼個人解讀HashMap原始碼
- 程式碼缺陷解讀:通用異常捕獲宣告缺陷漏洞
- 異常和中斷
- java中的異常Java
- Java 中的異常Java
- HashMap中面試常問的工作原理HashMap面試
- 【轉】java中異常與try catch finally詳解Java
- Ionic異常及解決
- HashMap原始碼解析和設計解讀HashMap原始碼
- SpringBoot中異常處理Spring Boot
- CentOS 常見異常及解決辦法CentOS
- SpringCloud BeanCurrentlyInCreationException 異常解決方案SpringGCCloudBeanException
- java異常你瞭解多少Java
- Day27:異常詳解
- 解讀鴻蒙輕核心的監控器:異常鉤子函式鴻蒙函式
- 異常中的異常——藉助系統異常處理特例實現匪夷所思的漏洞利用
- 異常和異常呼叫鏈
- 異常篇——異常記錄
- 異常篇——異常處理
- Swift 中 throws 異常丟擲Swift
- python中如何捕獲異常Python
- 詳解C#異常處理C#
- Reactor詳解之:異常處理React
- No bean named 'cacheManager' availablej 異常解決BeanAI
- 異常-異常的注意事項
- Java 異常(二) 自定義異常Java
- 如何避免ConcurrentModificationExceptionException
- 原始碼閱讀-HashMap原始碼HashMap
- HashMap 原始碼閱讀HashMap原始碼
- HashMap原始碼閱讀HashMap原始碼