Java 中使用迭代器時的 ConcurrentModificationException
如果任何特定方法在多執行緒 Java環境中檢測到併發資源時,可能會丟擲ConcurrentModificationException異常。在此過程中,物件在此處是一個不可允許的單元。
以下是一個與Java 中的 ConcurrentModificationException相關的示例:
Exception in thread "main" java.util.ConcurrentModificationException at java.base/java.util.ArrayList$Itr.checkForComodification(ArrayList.java:000) at java.base/java.util.ArrayList$Itr.next(ArrayList.java:000) atcom.journaldev.ConcurrentModificationException.ConcurrentModificationExceptio nExample.main(ConcurrentModificationExceptionExample.java:00)
對於此特定過程,異常在以下情況下可獲得:
檢測到異常並且迭代未在方法中定義。
當程序在使用名為 modCount 的內部標誌的快速失敗迭代器迴圈中阻塞時。
Java 中 ConcurrentModificationException 演算法
在此可能的演算法中,我們將向您展示如何在 Java 環境中執行 java.util.ConcurrentModificationException。透過使用此可能的演算法,我們將構建一些 Java 語法,透過它我們將進一步探索一些 Java 方法。
步驟 1 - 啟動程序。
步驟 2 - 宣告並匯入一些 Java 包以執行程序。
步驟 3 - 宣告一個公共類。
步驟 4 - 宣告一個字串引數。
步驟 5 - 建立一個數組列表物件的物件。
步驟 6 - 建立一個新的陣列列表。
步驟 7 - 填充陣列列表。
步驟 8 - 宣告一個 try 函式。
步驟 9 - 為列表宣告一個列印符號。
步驟 10 - 宣告一個迭代器類。
步驟 11 - 繼續獲取下一個值。
步驟 12 - 在迭代之間新增一個值。
步驟 13 - 列印更新後的陣列列表。
步驟 14 - 繼續獲取下一個值。
步驟 15 - 捕獲異常值。
步驟 16 - 列印異常值。
步驟 17 - 獲取值。
步驟 18 - 終止程序。
在 Java 中使用迭代器時的 ConcurrentModificationException 語法
for(int i = 0; i<myList.size(); i++){ System.out.println(myList.get(i)); if(myList.get(i).equals("3")){ myList.remove(i); i--; myList.add("6"); } } public static void main(String[] args) { ArrayList<Integer> list = new ArrayList<>(); list.add(ELEMENT); list.add(ELEMENT); list.add(ELEMENT); list.add(ELEMENT); list.add(ELEMENT); //Add more element if the user wants Iterator<Integer> it = list.iterator(); while (it.hasNext()) { Integer value = it.next(); System.out.println("List Value:" + value); if (value.equals(3)) list.remove(value); }{ HashMap<Integer, Integer> map = new HashMap<>(); map.put(ELEMENT 1, ELEMENT 1); map.put(ELEMENT 2, ELEMENT 2); map.put(ELEMENT 3, ELEMENT 3); Iterator<Integer> it = map.keySet().iterator(); while(it.hasNext()) { Integer key = it.next(); System.out.println("Map Value:" + map.get(key)); if (key.equals(SEARCH ELEMENT)){ map.put(ELEMENT 1, ELEMENT 4); } } } }
在上述可能的語法中,我們嘗試向您展示如何在 Java 環境中宣告和執行一個過程來實現 ConcurrentModificationException。透過使用這些語法,我們正在朝著與給定問題陳述相關的一些可能方法邁進。
遵循的方法
方法 1 - 使用迭代器類、iterator.hasNext() 和元素新增時的 Java 程式中的 ConcurrentModificationException
方法 2 - 透過使用 iterator.next() 函式說明 java.util.ConcurrentModificationException 方法的 Java 程式
方法 1:使用迭代器類、Iterator.hasNext() 和元素新增
迭代器類方法的使用
在此可能的方法中,我們將對特定集合應用迭代方法。當我們遍歷時,我們直接應用修改過程進行一些更正。當整個過程無法快速迭代時,在那個特定點發生異常。
for (Iterator<Integer> iterator = integers.iterator(); iterator.hasNext();) { Integer integer = iterator.next(); if(integer == 2) { iterator.remove(); } }
示例
//Java Program to ConcurrentModificationException while using Iterator class import java.util.ArrayList; import java.util.Iterator; public class ARBRDD { public static void main(String[] args){ ArrayList<Integer> list = new ArrayList<>(); list.add(1); list.add(2); list.add(3); list.add(4); list.add(5); Iterator<Integer> iterator = list.iterator(); while (iterator.hasNext()) { Integer value = iterator.next(); System.out.println("value: " + value); if (value.equals(2)){ System.out.println( "========================"); System.out.println("removing value: " + value); System.out.println( "========================"); list.remove(value); } } } }
輸出
value: 1 value: 2 ======================== removing value: 2 ======================== Exception in thread "main" java.util.ConcurrentModificationException at java.base/java.util.ArrayList$Itr.checkForComodification(ArrayList.java:1013) at java.base/java.util.ArrayList$Itr.next(ArrayList.java:967) at ARBRDD.main(ARBRDD.java:14)
Iterator.hasNext() 方法的使用
在此可能的方法中,我們將使用 iterator .hasNext() 應用修改過程,當迭代後發生修改時,異常會被丟擲。我們可以避免此異常時:
迭代完成後進行修改。
當我們應用同步塊或方法時。
示例
//Java Program to Avoid ConcurrentModificationException by directly using Iterator with iterator.hasNext() import java.util.ArrayList; import java.util.Iterator; public class ARBRDD { public static void main(String[] args){ ArrayList<Integer> list = new ArrayList<>(); list.add(1); list.add(2); list.add(3); list.add(4); list.add(5); Iterator<Integer> iterator = list.iterator(); while (iterator.hasNext()) { Integer value = iterator.next(); System.out.println("value: " + value); if (value.equals(2)){ System.out.println( "========================"); System.out.println("removing value: " + value); System.out.println( "========================"); iterator.remove(); } } } }
輸出
value: 1 value: 2 ======================== removing value: 2 ======================== value: 3 value: 4 value: 5
在列表中使用元素新增方法
在此可能的方法中,我們將向特定資料列表中新增一些資料元素。要從此過程中獲得 ConcurrentModificationException,我們可以新增一個新元素來更新列表並從中獲取所需的輸出。
示例
//Java program to illustrate the ConcurrentModificationException by using as an element is added during the iteration process import java.util.Iterator; import java.util.ArrayList; public class ARBRDDmodificationError { public static void main(String args[]){ ArrayList<String> arr = new ArrayList<String>(); arr.add("ARB"); arr.add("Bangladesh"); arr.add("RDD"); arr.add("India"); try { System.out.println( "ArrayList: "); Iterator<String> iter = arr.iterator(); while (iter.hasNext()) { System.out.print(iter.next() + "-;-"); System.out.println( "\n\nTrying to add" + " an element in " + "between iteration\n"); arr.add("Five"); } } catch (Exception e) { System.out.println(e); } } }
輸出
ArrayList: ARB-;-Trying to add an element in between iteration java.util.ConcurrentModificationException
方法 2
Iterator.next() 方法的使用
在此可能的方法中,我們將應用 iterator.next() 方法。當 Java 集合類失敗時,這意味著當某些執行緒遍歷迭代器時,集合更改了其過程。
HashSet<TableRecord> tableRecords = new HashSet<>(); ... for (Iterator<TableRecord> iterator = tableRecords.iterator(); iterator.hasNext(); ) { TableRecord record = iterator.next(); if (record.getDependency() == null) { for (Iterator<TableRecord> dependencyIt = tableRecords.iterator(); dependencyIt.hasNext(); ) { TableRecord dependency = dependencyIt.next(); if (dependency.getDependency() != null && dependency.getDependency().getId().equals(record.getId())) { tableRecords.remove(record); } } } }
示例
//Java program to illustrate the java.util.ConcurrentModificationException method by using the iterator.next() function import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; public class ConcurrentModificationExceptionARBRDDExample { public static void main(String args[]) { List<String> myList = new ArrayList<String>(); myList.add("16"); myList.add("07"); myList.add("10"); myList.add("2001"); myList.add("1997"); Iterator<String> it = myList.iterator(); while (it.hasNext()) { String value = it.next(); System.out.println("List Value Is Here. Have A Look @ARBRDD:- ->" + value); if (value.equals("2001")) myList.remove(value); } Map<String, String> myMap = new HashMap<String, String>(); myMap.put("16", "16"); myMap.put("07", "07"); myMap.put("1997", "1997"); Iterator<String> it1 = myMap.keySet().iterator(); while (it1.hasNext()) { String key = it1.next(); System.out.println("Map Value:" + myMap.get(key)); if (key.equals("07")) { myMap.put("16", "2001"); } } } }
輸出
List Value Is Here. Have A Look @ARBRDD:-->16 List Value Is Here. Have A Look @ARBRDD:-->07 List Value Is Here. Have A Look @ARBRDD:-->10 List Value Is Here. Have A Look @ARBRDD:-->2001 Map Value:1997 Map Value:16 Map Value:07
結論
當特定物件嘗試以併發方式修改自身時,同時在特定程式中使用迭代器類,可能會遇到 ConcurrentModificationException。今天在這篇文章中,我們學習了併發方法的修改。透過使用上面提到的語法和演算法,我們構建了一些 Java 程式碼以有效地解決問題陳述。
另請參閱: Java 面試問題及答案