Java中的ConcurrentModificationException及示例
在 Java 中,如果在 多线程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 - 创建一个ArrayList对象。
步骤 6 - 创建一个新的ArrayList。
步骤 7 - 填充ArrayList。
步骤 8 - 声明一个try函数。
步骤 9 - 为列表声明一个打印符号。
步骤 10 - 声明一个迭代器类。
步骤 11 - 继续获取下一个值。
步骤 12 - 在迭代之间添加一个值。
步骤 13 - 打印更新后的ArrayList。
步骤 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 - Java程序通过在迭代过程中添加元素并执行修改来说明ConcurrentModificationException
方法 2 - Java程序通过使用iterator.next()、remove()函数以及部署循环来说明java.util.ConcurrentModificationException方法
方法 1:使用迭代过程并执行修改过程
元素添加方法的使用
在这种可能的方法中,我们将对特定集合应用迭代方法。当我们遍历时,我们直接应用修改过程来进行一些更正。当整个过程无法快速迭代时,在那个特定点会发生异常。
for (Iterator<Integer> iterator = integers.iterator(); iterator.hasNext();) {
Integer integer = iterator.next();
if(integer == 2) {
iterator.remove();
}
}
示例
//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
迭代器修改方法的使用
在这种可能的方法中,我们将应用修改过程,当异常作为迭代后的修改被引发时。当我们这样做时,我们可以避免此异常:
迭代结束后再进行修改。
当我们应用同步块或方法时。
示例
//Java program to show ConcurrentModificationException when exception is raised as a modification is done after the iteration
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 Is Here: ");
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: "
+ arr.add("Going Soon"));
System.out.println(
"\nUpdated ArrayList Is Here: ");
iter = arr.iterator();
while (iter.hasNext()) {
System.out.print(iter.next()
+ "-;-");
}
}
catch (Exception e) {
System.out.println(e);
}
}
}
输出
ArrayList Is Here: ARB_;_BANGLADESH_;_RDD_;_INDIA_;_Trying to add an element in between iteration: true Updated ArrayList Is Here: ARB-;-BANGLADESH-;-RDD-;-INDIA-;-Going Soon-;-
方法 2:使用Iterator.next()、Remove()函数以及部署循环
Iterator.next()方法的使用
在这种可能的方法中,我们将应用iterator.next()方法。当Java集合类失败时,这意味着集合在某些线程遍历迭代器时改变了其过程。
@Test(expected = ConcurrentModificationException.class)
public void whilstRemovingDuringIteration_shouldThrowException() throws
InterruptedException {
List<Integer> integers = newArrayList(1, 2, 3);
for (Integer integer : integers) {
integers.remove(1);
}
}
List<Integer> integers = newArrayList(1, 2, 3);
List<Integer> toRemove = newArrayList();
for (Integer integer : integers) {
if(integer == 2) {
toRemove.add(integer);
}
}
integers.removeAll(toRemove);
assertThat(integers).containsExactly(1, 3);
示例
//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 program to illustrate and deploy a loop to avoid java.util.ConcurrentModificationException in an array list
import java.util.ArrayList;
import java.util.List;
public class ConcurrentModificationExceptionWithArrayListSubList {
public static void main(String[] args) {
List<String> names = new ArrayList<>();
names.add("Kollam");
names.add("Dhaka");
names.add("Chennai");
names.add("Palani");
List<String> first2Names = names.subList(0, 2);
System.out.println(names + " , " + first2Names);
names.set(1, "Kolkata");
System.out.println(names + " , " + first2Names);
names.add("Kochi");
System.out.println(names + " , " + first2Names);
}
}
输出
[Kollam, Dhaka, Chennai, Palani] , [Kollam, Dhaka] [Kollam, Kolkata, Chennai, Palani] , [Kollam, Kolkata] Exception in thread "main" java.util.ConcurrentModificationException at java.base/java.util.ArrayList$SubList.checkForComodification(ArrayList.java:141 5) at java.base/java.util.ArrayList$SubList.listIterator(ArrayList.java:1284) at java.base/java.util.AbstractList.listIterator(AbstractList.java:313) at java.base/java.util.ArrayList$SubList.iterator(ArrayList.java:1280) at java.base/java.util.AbstractCollection.toString(AbstractCollection.java:451) at java.base/java.lang.String.valueOf(String.java:4225) at ConcurrentModificationExceptionWithArrayListSubList.main(ConcurrentModification ExceptionWithArrayListSubList.java:16)
Remove()迭代器方法的使用
在这种可能的方法中,我们将应用remove()方法。通过使用它,我们将从列表中删除相同的对象,而不是任何对象。
示例
//Java program to avoid the ConcurrentModificationException in single-threaded environment by using the iterator to remove() a function
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;
public class AvoidConcurrentModificationException{
public static void main(String[] args){
List<String> myList = new CopyOnWriteArrayList<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:" + value);
if (value.equals("3")) {
myList.remove("4");
myList.add("6");
myList.add("7");
}
}
System.out.println("List Size Is Now :" + myList.size());
Map<String, String> myMap = new ConcurrentHashMap<String,
String>();
myMap.put("16", "07");
myMap.put("2", "16");
myMap.put("2022", "13110");
Iterator<String> it1 = myMap.keySet().iterator();
while (it1.hasNext()) {
String key = it1.next();
System.out.println("Map Value Is Here:" + myMap.get(key));
if (key.equals("1")) {
myMap.remove("3");
myMap.put("4", "4");
myMap.put("5", "5");
}
}
System.out.println("Map Size Is Here. Have A Look:" +
myMap.size());
}
}
输出
List Value:16 List Value:07 List Value:10 List Value:2001 List Value:1997 List Size Is Now :5 Map Value Is Here:16 Map Value Is Here:07 Map Value Is Here:13110 Map Size Is Here. Have A Look:3
结论
当特定对象尝试以并发方式修改自身时,可能会遇到ConcurrentModificationException。在今天的文章中,我们学习了并发方法的修改。通过使用上述语法和算法,我们构建了一些Java代码以有效地解决问题陈述。
另请阅读: Java面试问题及答案
数据结构
网络
关系数据库管理系统 (RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP