相关疑难解决方法(0)

ConcurrentModificationException和HashMap

我正在使用JPA持久化对象.Main对象与另一个对象具有一个拥有的One-Many关系.另一个对象存储在HashMap中.什么样的同步可以解决这个问题?它似乎发生在完全随机的时间,并且非常难以预测.这是我得到的例外:

Exception in thread "pool-1-thread-1" java.util.ConcurrentModificationException
        at java.util.HashMap$HashIterator.nextEntry(Unknown Source)
        at java.util.HashMap$ValueIterator.next(Unknown Source)
        at org.hibernate.collection.AbstractPersistentCollection$IteratorProxy.next(AbstractPersistentCollection.java:555)
        at org.hibernate.engine.Cascade.cascadeCollectionElements(Cascade.java:296)
        at org.hibernate.engine.Cascade.cascadeCollection(Cascade.java:242)
        at org.hibernate.engine.Cascade.cascadeAssociation(Cascade.java:219)
        at org.hibernate.engine.Cascade.cascadeProperty(Cascade.java:169)
        at org.hibernate.engine.Cascade.cascade(Cascade.java:130)
Run Code Online (Sandbox Code Playgroud)

java collections exception concurrentmodification

118
推荐指数
4
解决办法
14万
查看次数

奇怪的Java并发修改异常示例

如果我们这样写,就会出现并发修改异常:

public static void main(String... args) {
    List<String> listOfBooks = new ArrayList<>();
    listOfBooks.add("Programming Pearls");
    listOfBooks.add("Clean Code");
    listOfBooks.add("Effective Java");
    listOfBooks.add("Code Complete");

    System.err.println("Before deleting : " + listOfBooks);
    for (String book : listOfBooks) {
        if (book.contains("Code")) {
            listOfBooks.remove(book);
        }
    }
    System.err.println("After deleting : " + listOfBooks);
}
Run Code Online (Sandbox Code Playgroud)

另一方面,如果我们这样写,没有并发修改异常! 请注意,代码完全相同,除了用于比较的字符串,在第一个示例中它是一个代码,在第二个示例中它是一个Java

public static void main(String... args) {
    List<String> listOfBooks = new ArrayList<>();
    listOfBooks.add("Programming Pearls");
    listOfBooks.add("Clean Code");
    listOfBooks.add("Effective Java");
    listOfBooks.add("Code Complete");

    System.err.println("Before deleting : " + listOfBooks);
    for (String book : …
Run Code Online (Sandbox Code Playgroud)

java exception

8
推荐指数
2
解决办法
405
查看次数

出现 java.util.ConcurrentModificationException

也许这可能是一个重复的问题,对此我很抱歉。但我的问题没有解决。

当 foreach 循环中第二次执行时,下面的代码给了我这个异常(java.util.ConcurrentModificationException)。即使我使用迭代器删除了 arraylist 对象。

@RequestMapping("edit")
public ModelAndView editItemToInvoice(HttpSession session,@RequestParam("itemname")String itemname){

    ArrayList<InvoiceEntities> show=(ArrayList<InvoiceEntities>)session.getAttribute("invoices");

    if(show==null){
        show=new ArrayList<InvoiceEntities>();
    }

    ArrayList<InvoiceEntities> edit=new ArrayList<InvoiceEntities>();

    for(InvoiceEntities itemnam:show){
        if(itemnam.getItemName().equals(itemname)){
            int index=show.indexOf(itemnam);
            edit.add(show.get(index));

            Iterator<InvoiceEntities> iter = show.iterator();
            while(iter.hasNext()){
                InvoiceEntities getitem=iter.next();
                if(getitem.getItemName().equals(itemname)){
                    iter.remove();
                    //break;
                }
            }

        }
    }

    System.out.println(session.getAttribute("invoices"));

    ModelAndView model=new ModelAndView();
    session.setAttribute("invoices", show);
    model.addObject("editobj",edit);
    model.addObject("items",session.getAttribute("invoices"));
    model.setViewName("jsp/Invoice");

    return model;
}
Run Code Online (Sandbox Code Playgroud)

异常是 java.util.ConcurrentModificationException

 SEVERE: Servlet.service() for servlet [spring-dispatcher] in context with  
 path [/Invoice] threw exception [Request processing failed; nested 
 exception is java.util.ConcurrentModificationException] with root cause
java.util.ConcurrentModificationException
at java.util.ArrayList$Itr.checkForComodification(ArrayList.java:901) …
Run Code Online (Sandbox Code Playgroud)

java jakarta-ee

5
推荐指数
1
解决办法
1万
查看次数

从循环内的列表中删除元素

我正在尝试从循环内的 ArrayList 中删除一个元素。

还行吧。

ArrayList<Integer> list = new ArrayList<Integer>(Arrays.asList(1, 2, 3));
for(Integer i: list){
    if(i == 2)
        list.remove(i);
}
Run Code Online (Sandbox Code Playgroud)

但这不是,并抛出concurrentMODificationException。

 ArrayList<Integer> list = new ArrayList<Integer>(Arrays.asList(1, 2, 3));
 for(Integer i: list){
        list.remove(i);
 }
Run Code Online (Sandbox Code Playgroud)

我不明白为什么。

我刚刚添加了另一个元素,它也不行(抛出 concurrentMODificationException)。

ArrayList<Integer> list = new ArrayList<Integer>(Arrays.asList(1, 2, 3, 4));

System.out.println(list);

for (Integer i : list) {
    if (i == 2)
        list.remove(i);
}
Run Code Online (Sandbox Code Playgroud)

java arraylist

4
推荐指数
1
解决办法
8289
查看次数

ConcurrentModificationException和HashSet.iterator()

我有一个for循环

      for (int neighbour : neighbours) {
Run Code Online (Sandbox Code Playgroud)

我可以neighbours在循环内修改的地方.发现那是事业的原因ConcurrentModificationException.并阅读/sf/answers/573266921/

因此,如果您想要修改列表(或一般的任何集合),请 使用迭代器,因为它知道修改,因此将正确处理.

所以我尝试过:

neighboursItr = neighbours.iterator();
while (neighboursItr.hasNext()) {
  // try disconnecting vertices
  neighbour = neighboursItr.next();
Run Code Online (Sandbox Code Playgroud)

但这并没有解决问题.为什么?

java iterator concurrentmodification

3
推荐指数
1
解决办法
9088
查看次数

Java LinkedList并发修改异常

我为 LinkedList 编写了一个简单的代码,将数据添加到其中并对其进行迭代,
我导入了 LinkedList 和 Iterator 所需的包,代码中没有错误,

我收到以下异常 -

Exception in thread "main" java.util.ConcurrentModificationException
    at java.util.LinkedList$ListItr.checkForComodification(Unknown Source)
    at java.util.LinkedList$ListItr.next(Unknown Source)
    at collectionFramework.Demo.displayList(LinkedListDemo.java:38)
    at collectionFramework.LinkedListDemo.main(LinkedListDemo.java:13)
Run Code Online (Sandbox Code Playgroud)

这是我的代码 -

package collectionFramework;

import java.util.Iterator;
import java.util.LinkedList;

public class LinkedListDemo{

    public static void main(String[] args) {

        Demo obj = new Demo();

        obj.addToList();
        obj.displayList();
    }

}

class Demo{

    LinkedList<String> al;
    Iterator<String> itr;  

    Demo(){
        al = new LinkedList<String>();
        itr = al.iterator();
    }

    void addToList(){
          al.add("Aniruddha");  
          al.add("Hitesh");  
          al.add("Rahul");  
          al.add("Kshitij");  
    }

    void displayList(){

        while(itr.hasNext()){  
               System.out.println(itr.next());  
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

java iterator linked-list

3
推荐指数
1
解决办法
2858
查看次数