标签: concurrentmodification

在Java中防止零星的ConcurrentModificationException的最佳做法是什么?

迭代列表或映射的代码序列会阻止ConcurrentModificationException?我们的代码中有反复和零星的ConcurrentModificationException.这个问题有两个原因.

  1. 另一个线程在迭代时更改列表
  2. 在循环中调用的方法是更改​​列表.

问题1可以通过循环同步来解决.但是,如果在循环中调用外来代码(例如原因2),则这很糟糕.

问题2可以通过列表或地图的副本来解决.

这意味着列表或映射必须在循环之前复制到同步块中.有更好的解决方案吗?

一些示例代码:

public void todoSomeThings( Map<Abc, Object> map ){
    for( Abc abc : map.keySet() ){
        abc.todoSomeThings();
    }
}
Run Code Online (Sandbox Code Playgroud)

java thread-safety concurrentmodification

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

我得到以下异常:"java.util.ConcurrentModificationException"

当我运行这段代码时,我收到了"发生异常:java.util.ConcurrentModificationException".有没有人在这看到问题是什么?

public void mudaDeEstado() {
    Luz luz = new Luz();
    while(this.iterador.hasNext()) {
        luz = (this.iterador.next());
        luz.defineEstado(!luz.acesa());
    }

}
Run Code Online (Sandbox Code Playgroud)

非常感谢!!

java concurrentmodification

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

如何在迭代时向列表添加值

我有这样的场景

List<String> xxx = new ArrayList
for(String yyy : xxx){
   for(String zzz:xyxy){
     if(!zzz.equals(yyy)){
       xxx.add(zzz);
     }
   }
}
Run Code Online (Sandbox Code Playgroud)

但我得到java.util.ConcurrentModificationException:null exception.Can任何人都可以帮我解决这个问题.任何人都可以给我替代方法来执行此操作吗?

java concurrentmodification

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

在循环期间修改集合时,它无法成为C#

下面的代码的foreach语句抛出InvalidOperationException,说"集合已被修改.枚举操作无法执行".我看不出这是怎么可能的,因为颜色在初始化后永远不会被修改.

Dictionary<Color, int> colorDictionary = new Dictionary<Color, int>();
//Put stuff in colorDictionary...
int currentBest = 257;
Color replaceColor = Color.Empty;
Dictionary<Color, int>.KeyCollection colors = colorDictionary.Keys;
foreach (Color c in colors)
{
    if (colorDictionary[c] == 0)
    {
        continue;
    }
    if (ColorDistance(color, c) < currentBest)
    {
        replaceColor = c;
        colorDictionary[c]--;
    }
}
Run Code Online (Sandbox Code Playgroud)

c# collections invalidoperationexception concurrentmodification

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

迭代集合以添加项目但抛出 ConcurrentModificationException

package com.ripal;

import java.util.ArrayList;

import java.util.Collections;

import java.util.Iterator;

public class Outputs {

    public void show() {
        final ArrayList<String> list = new ArrayList<String>();

        list.add("banana");
        list.add("apple");

        Iterator<String> itr = list.iterator();

        Collections.sort(list);
        while (itr.hasNext()) {
            System.out.println(itr.next() + " ");
        }
    }
}

class Test {

    public static void main(String[] args) {
        Outputs outputs = new Outputs();
        outputs.show();
    }
}
Run Code Online (Sandbox Code Playgroud)

java collections arraylist concurrentmodification

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

hashmap获取并发修改异常的问题

我在使用map并执行一些删除时遇到以下错误.如何避免这种情况?

Caused by: java.util.ConcurrentModificationException
    at java.util.HashMap$HashIterator.nextEntry(HashMap.java:793)
    at java.util.HashMap$EntryIterator.next(HashMap.java:834)
    at java.util.HashMap$EntryIterator.next(HashMap.java:832)
Run Code Online (Sandbox Code Playgroud)


   Map<FormField, Object> ItemMap = domainItem.getValues();         
   for (Map.Entry<FormField, Object> ValMap : ItemMap.entrySet()) {         
       List<Field> groupIdList = Mapper.getGroupId(groupFieldId);           
       for (Field field : groupIdList) {
           ItemMap.put(new FormField(field), domainItem.getDomainItemLinkId());
       }
       ItemMap.remove(ValMap.getKey());
   }
Run Code Online (Sandbox Code Playgroud)

java hashmap concurrentmodification

0
推荐指数
1
解决办法
6223
查看次数

java泛型运行时error.java.util.ConcurrentModificationException

我有以下示例java泛型代码,我根据StackOverflow上的人的建议修改了.现在编译正在进行.

import java.util.*;

public class GenericBox<T>
{

        private List<T> tList;
        private Iterator<T> itor;

        public GenericBox()
        {
                tList = new ArrayList<T>();
                itor = tList.listIterator();
        }

        public void insert(T element)
        {
                tList.add(element);
        }

        public T retrieve()
        {
                if(itor.hasNext())
                {
                        return itor.next();
                }
                return null;
        }

        public static void main (String [] args)
        {

                GenericBox <String> strbox = new GenericBox<String>();
                GenericBox <String> intbox = new GenericBox<String>();

                strbox.insert(new String("karthik"));
                strbox.insert(new String("kanchana"));
                strbox.insert(new String("aditya"));


                String s = strbox.retrieve();
                System.out.println(s);

                s = strbox.retrieve();
                System.out.println(s);

                s = …
Run Code Online (Sandbox Code Playgroud)

java concurrentmodification

0
推荐指数
1
解决办法
226
查看次数

从Java中删除ArrayList中的对象出错[Eclipse]

我有这个方法从ArrayList 中删除特定的对象P.

这是我的代码:

public void removeProduct(Product p) throws StockException{
        int flag=0;
        for(Product i:StockProducts)
            if(p.getCode()==i.getCode()){
                this.StockProducts.remove(p);
                flag=1;
            }

        if(flag==0){
                StockException e = new StockException("could not remove the Product , PRODUCT IS NOT IN THE STOCK:  ", p);
        throw e;
          }
    }
Run Code Online (Sandbox Code Playgroud)

错误:

Exception in thread "main" java.util.ConcurrentModificationException
    at java.util.ArrayList$Itr.checkForComodification(Unknown Source)
    at java.util.ArrayList$Itr.next(Unknown Source)
    at Stock.removeProduct(Stock.java:29)
    at Test.main(Test.java:18)
Run Code Online (Sandbox Code Playgroud)

如果您需要有关我的代码的更多信息,请告诉我

添加方法

public void addProduct(Product p) throws StockException{
        for(Product i:StockProducts)
            if(p.getCode()==i.getCode()){
                        StockException e = new StockException("could not add the Product , CODE ALREADY …
Run Code Online (Sandbox Code Playgroud)

java collections arraylist concurrentmodification

0
推荐指数
1
解决办法
1858
查看次数

foreach循环中的ConcurrentModificationException

在我的代码中:

    Collection<String> c = new ArrayList<>();
    Iterator<String> it = c.iterator();
    c.add("Hello");
    System.out.println(it.next());
Run Code Online (Sandbox Code Playgroud)

发生异常,因为我的集合在创建迭代器后发生了变化.

但是在这段代码中呢:

 ArrayList<Integer> list = new ArrayList<Integer>();
    list.add(1);
    list.add(2);
    list.add(3);
    for (Integer integer : list) {     // Exception is here
        if (integer.equals(2)) {
            list.remove(integer);
        }
    }
Run Code Online (Sandbox Code Playgroud)

为什么发生例外?

在第二个代码中,我在for-each循环之前对我的集合进行了更改.

java collections arraylist concurrentmodification

0
推荐指数
1
解决办法
6224
查看次数

有时会自动调用repaint()吗?

我正在制作游戏.游戏中的每个对象都在注册表中注册.我有一个游戏循环调用的更新和渲染方法.render方法调用repaint()JPanel,我已经覆盖paintComponent(Graphics g)了图形.当它呈现并更新时,它会遍历每个对象并对其进行更新并从注册表中呈现它.有时候我会得到一个并发修改错误,我相信这是因为有些事情是在paintComponent()我的游戏循环中调用另一个.如果一个JPanel或甚至JFrame调用自己的重绘,有没有办法禁用它?

java graphics swing paintcomponent concurrentmodification

0
推荐指数
1
解决办法
188
查看次数