简体   繁体   English

ConcurrentModificationException从迭代器移除元素

[英]ConcurrentModificationException removing element from iterator

A ConcurrentModificationException was returned removing an entry from a map but I am unable to see what's wrong. 返回了ConcurrentModificationException,从地图上删除了一个条目,但是我看不到有什么问题。

    Iterator<String> mapIterator = this.attributeMap.keySet().iterator();
    while (mapIterator.hasNext())
    {
        String attributeName = mapIterator.next();
        if (attributeName.matches(pattern))
        {
            mapIterator.remove();
        }
    }

You can check this out on some details about CME: https://docs.oracle.com/javase/7/docs/api/java/util/ConcurrentModificationException.html 您可以查看有关CME的一些详细信息: https : //docs.oracle.com/javase/7/docs/api/java/util/ConcurrentModificationException.html

This exception may be thrown by methods that have detected concurrent modification of an object when such modification is not permissible. 当不允许对对象进行同时修改时,检测到该对象的同时修改的方法可能会引发此异常。 For example, it is not generally permissible for one thread to modify a Collection while another thread is iterating over it. 例如,通常不允许一个线程修改Collection而另一个线程对其进行迭代。 In general, the results of the iteration are undefined under these circumstances. 通常,在这些情况下,迭代的结果是不确定的。 Some Iterator implementations (including those of all the general purpose collection implementations provided by the JRE) may choose to throw this exception if this behavior is detected. 如果检测到此行为,则某些Iterator实现(包括JRE提供的所有通用集合实现的实现)可能会选择抛出此异常。 Iterators that do this are known as fail-fast iterators, as they fail quickly and cleanly, rather that risking arbitrary, non-deterministic behavior at an undetermined time in the future. 执行此操作的迭代器称为快速失败迭代器,因为它们会快速干净地失败,而不是在未来的不确定时间内冒任意,不确定的行为的风险。

Here are some solutions provided by Baeldung, linked below: 以下是Baeldung提供的一些解决方案,链接如下:

  1. Using an Iterator Directly 直接使用迭代器
  2. Not removing during iteration 在迭代过程中不删除
  3. Using removeIf() 使用removeIf()
  4. Filtering using Streams 使用流过滤

You can check his site out for the details for each solution to avoid CME: https://www.baeldung.com/java-concurrentmodificationexception 您可以在他的网站上查看每种解决方案的详细信息,以避免使用CME: https//www.baeldung.com/java-concurrentmodificationexception

Hope this helps. 希望这可以帮助。

In this case the line mapIterator.next() throws ConcurrentModificationException . 在这种情况下,行mapIterator.next()抛出ConcurrentModificationException

The reason behind this is: 其背后的原因是:
There is an int variable modCount which provides the number of times list size has been changed, this value is used in every next() call to check for any modifications in a function checkForComodification() . 有一个int变量modCount ,它提供了更改列表大小的次数,此值在每个next()调用中使用,以检查功能checkForComodification()是否有任何修改。
if mapIterator.next() found change in modCount while iteration object then it will throw ConcurrentModificationException . 如果mapIterator.next()在迭代对象中发现modCount发生变化,则它将抛出ConcurrentModificationException

To avoid this please follow the below points: 为避免这种情况,请遵循以下几点:

  1. You can convert the list to an array and then iterate on the array. 您可以将列表转换为数组,然后在数组上进行迭代。 This approach works well for small or medium size list but if the list is large then it will affect the performance a lot. 这种方法适用于中小型列表,但是如果列表很大,则对性能的影响很大。

  2. You can lock the list while iterating by putting it in a synchronized block. 您可以通过将列表放在同步块中来在迭代时锁定列表。 This approach is not recommended because it will cease the benefits of multithreading. 不建议使用此方法,因为它将停止多线程的好处。

  3. If you are using JDK1.5 or higher then you can use ConcurrentHashMap and CopyOnWriteArrayList classes. 如果使用的是JDK1.5或更高版本,则可以使用ConcurrentHashMap和CopyOnWriteArrayList类。 It is the recommended approach. 这是推荐的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么使用迭代器从列表中删除元素会导致ConcurrentModificationException? - Why removing element from list using iterator causes ConcurrentModificationException? ConcurrentModificationException 使用列表迭代器 java 删除元素时 - ConcurrentModificationException When removing element using list iterator java 删除ArrayList中的元素时发生ConcurrentModificationException [使用iterator.remove()] - ConcurrentModificationException in removing element in ArrayList [Using iterator.remove()] 从HashMap中删除元素时发生异常java.util.ConcurrentModificationException - Exception when removing element from HashMap java.util.ConcurrentModificationException 通过Iterator从ArrayList中删除元素 - Removing element from ArrayList through Iterator 使用迭代器从 ArrayList 中删除元素 - Removing element from ArrayList using Iterator ConcurrentModificationException,但不从ArrayList中删除任何内容 - ConcurrentModificationException but not removing anything from ArrayList 与迭代器的ConcurrentModificationException - ConcurrentModificationException with iterator 迭代器内部的迭代器ConcurrentModificationException - Iterator inside Iterator ConcurrentModificationException 为什么从arraylist remove方法中删除倒数第二个元素不会抛出ConcurrentModificationException? - Why removing second last element from arraylist remove method doesn't throw ConcurrentModificationException?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM