Java编程语言提供了一系列异常处理案例,并发修改异常就是其中之一。当程序中的线程尝试修改当前进程中没有编辑权限的对象时,会发生并发修改异常。简单来说,当我们尝试编辑当前正在被另一个进程使用的对象时,就会出现 ConcurrentModificationException。
开始您的免费软件开发课程
网络开发、编程语言、软件测试及其他
一个非常简单的例子是一个线程正在处理的集合。正在处理的集合不允许被修改,因此会抛出 ConcurrentModificationException。
语法:
下面是异常的最简单语法。 ConcurrentModificationException 是 java lang RunTimeException 的一部分,并且扩展了它。
public class Concurrent Modification Exception extends Runtime Exception
我们在Java中创建对象时,根据自己的需要设置一些限制和权限,这是可以理解的。但有时,我们试图在线程中更改值或修改列表,这时就会出现 ConcurrentModificationException,因为该对象已被另一个线程使用,无法编辑或修改。
了解此异常并不总是意味着有一个对象被同时修改也很重要。在单线程的情况下,如果发生方法调用,就会抛出此异常,这在某种程度上违反了对象契约。
构造函数:
ConcurrentModificationException 的一个非常基本的构造函数如下所示:ConcurrentModificationException ()。除了简单的构造函数之外,我们还有以下贡献者,可以根据需要使用:
现在我们已经了解了并发修改异常是什么以及它是如何工作的,让我们开始实现。现在我们将通过一个例子来理解异常;例如,我们有一个数组列表,随着一些操作,我们将尝试修改它,这将导致异常。示例代码如下:
代码:
import java.awt.List; import java.util.*; public class ConcurrentModificationException { public static void main ( String[] args) { ArrayList<Integer> Numberlist = new ArrayList<Integer> () ; Numberlist.add ( 1) ; Numberlist.add ( 2) ; Numberlist.add ( 3) ; Numberlist.add ( 4) ; Numberlist.add ( 5) ; Iterator<Integer> it = Numberlist.iterator () ; while ( it.hasNext () ) { Integer numbervalue = it.next () ; System.out.println ( "List Value:" + numbervalue) ; if ( numbervalue .equals ( 3) ) Numberlist.remove ( numbervalue) ; } } }
代码说明:从几个导入文件开始,然后类减速和main方法。初始化数组列表并添加一个不断添加数字的功能;这样,它将正在进行中。每次添加时,我们都会打印列表值。但是接下来我们有一个 if 循环,它将查找等于 3 的数字,一旦找到 3,它将尝试删除该数字,这就是遇到异常的地方,并且程序将被终止。列表中不会再添加任何内容。
如果执行上面的代码没有任何错误,代码将偶然发现异常。该异常位于 ConcurrentModificationException.main (ConcurrentModificationException.java:14)。这个异常的原因很明显,我们试图修改一个列表,Numberlist。请参阅下面所附的屏幕截图以获取正确的输出:
正如你所看到的,程序按照我们的预期执行了,打印语句成功执行,直到遇到值 3。程序流程在值 3 处中断,因为我们有一个 if 循环,看起来等于 3 的值并将其删除。但迭代器已经在进行中,尝试删除 3 将失败,并且会抛出异常 ConcurrentModificationException。
出于第二个示例的目的,我们将尝试执行一个能够成功避开并发修改异常的程序。在相同的代码中,如果我们尝试在进程中修改数组列表,它将捕获 ConcurrentModificationException,并且程序将被终止。第二个例子的代码如下:
代码:
import java.util.Iterator; import java.util.ArrayList; public class ConcurrentModificationException { public static void main ( String args[]) { ArrayList<String> arraylist = new ArrayList<String> ( ) ; arraylist.add ( "One") ; arraylist.add ( "Two") ; arraylist.add ( "Three") ; arraylist.add ( "Four") ; try { System.out.println ( "ArrayList: ") ; Iterator<String> iteratornew = arraylist.iterator () ; while ( iteratornew.hasNext ( ) ) { System.out.print ( iteratornew.next () + ", "); } System.out.println ( "\n\n Trying to add an element in between iteration:" + arraylist.add ( "Five") ) ; System.out.println ( "\n Updated ArrayList: \n") ; iteratornew = arraylist.iterator () ; while ( iteratornew.hasNext ( ) ) { System.out.print ( iteratornew.next () + ", "); } } catch ( Exception e) { System.out.println ( e) ; } } }
Code Explanation: Similar to the earlier program, we have the required files and methods. We are simply adding a few elements to the array list, and it is iterating; we add another element, which is Five. We will print the updated list upon the latest additions, which will have our newly added element. We have implemented the try-catch block to catch the exception. If any exception occurs, it will print the exception.
Output:
As you can see in the output, the first array list is plain, with few elements. Then in the next line, we attempt to add an element while in iteration, the element is “Five”. It is successfully added, and it prints true for the sentence. And in the next line, we print an updated list, which has the Five-element. We have updated the list after the process is ending; if we try to add the element while in the process, it will throw the ConcurrentModificationException, and the program will cease.
Avoiding Concurrent Modification Exception is possible on a different level of threads. To avoid a Concurrent Exception with a single thread environment, you can remove the object from the iterator and modify it, use remove ().
And in the case of a multi-thread environment, you have few choices like converting the list in the process into an array and then working on the array. Then you can also put a synchronized block over a list and lock it. But locking the list is not recommended as it may limit the advantages of multi-threading programming. Using a sublist to modify a list will result in nothing. Out of all the ways to avoid, the most preferred way is to wait until the execution is finished and then edit or modify the list or the object.
When a process is using one resource and another process attempts to edit or amend it, the exception ConcurrentModificationException will be thrown. The simplest method to avoid is to modify the list after iteration. Many methods are listed to avoid the exception but advised to implement them with small programs.
以上是Java并发修改异常的详细内容。更多信息请关注PHP中文网其他相关文章!