标签: concurrentmodification

打印向量内容时出现 ConcurrentModificationException

好吧,我想编写一个程序,允许用户输入无限数量的数字,然后再次以相同的顺序打印它们。

我收到此错误:

Exception in thread "main" java.util.ConcurrentModificationException
    at java.util.Vector$Itr.checkForComodification(Unknown Source)
    at java.util.Vector$Itr.next(Unknown Source)
    at myPackage.Main.main(Main.java:41)
Run Code Online (Sandbox Code Playgroud)

第 41 行是这一行“System.out.println(itr.next());” 一

这是我的代码:

package myPackage;

import java.util.Scanner;
import java.util.Vector;
import java.lang.Integer;
import java.util.Iterator;

public class Main {

private static Scanner user_input;

public static void main(String[] args) {

    int first = 42;
    int second = 84;

    user_input = new Scanner(System.in);

    Vector<Integer> v = new Vector<Integer>();

    Iterator<Integer> itr = v.iterator();

    System.out.println("Please enter the numbers you wish to store temporarily before printing. When finished, enter either …
Run Code Online (Sandbox Code Playgroud)

java concurrentmodification

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

For 循环中的 java.util.ConcurrentModificationException

我正在尝试编写一个IM软件,我想让用户离开对话并告诉他的伙伴他已经离开...我更喜欢使用for循环而不是迭代器,寻找所有用户并获取要求离开的用户并像这样删除他:

   for(Clientuser Cu: EIQserver.OnlineusersList)
          if(Cu.ID.equals(thsisUser.ID)) // find the user who ask to leave 
          {
          Omsg.setBody("@@!&$$$$@@@####$$$$"); //code means : clien! ur parter leaves...
                 sendMessage(Omsg); // sed message to thje partner with that code
                 EIQserver.OnlineusersList.remove(Cu);// remove the partner
                EIQserver.COUNTER--;// decrease counter.

          }
Run Code Online (Sandbox Code Playgroud)

我得到异常:java.util.ConcurrentModificationException

我正在使用迭代器,为了摆脱这个异常,我转换为 for,但同样的异常仍然出现!我怎样才能摆脱这个异常?

java for-loop concurrentmodification

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

可以在 HashMap 中获取、放置和删除元素而不进行迭代导致 ConcurrentModificationException 吗?

我有一个静态 hashMap,与多个线程共享。我根本没有迭代地图,只是使用get, put, remove。安全吗ConcurrentModificationException

该方法看起来像这样

private static Map<Long, Integer> TRACKER = new HashMap<Long,Integer>();
public static void track(Long tid, boolean b) {
        if (b) {
            if (TRACKER.containsKey(tid)) {
                TRACKER.put(tid, TRACKER.get(tid) + 1);
            } else {
                TRACKER.put(tid, 1);
            }
        } else {
            Integer n = TRACKER.get(tid);
            if (n != null) {
                n = n -1;
                if (n == 0) {
                    TRACKER.remove(tid);
                } else {
                    TRACKER.put(tid, n);
                }
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

java hashmap concurrentmodification

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

Java是否具有支持并发修改的数据结构?

我正在用Java制作游戏.游戏中的每个敌人都是一个线程,他们不断循环游戏的数据结构(我总是使用Vector类).

最近我得到了"ConcurrentModificationException",因为当一个Thread循环遍历Vector时,正在向Vector添加/删除元素.我知道有一些策略可以避免添加/删除问题(我实际上使用了一些来避免删除问题,但我仍然遇到"添加"问题).

我听说java支持一个避免ConcurrentModificationException的Vector/List.

你知道这个结构可能是什么吗?谢谢.

java multithreading concurrentmodification data-structures

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

并发修改异常java,我不认为我可以在这里使用iterator?

我有一个列表,我需要迭代并删除某些项目.我不能使用迭代器,因为我需要调用每个项目(例如ls.getStatus())的方法,这些方法不适用于迭代器.如果ls.getStatus() == 0我需要删除该项目.我怎么能避免ConcurrentModificationException

for (MyList ls : list) {
    if (ls.getStatus() == 0) {
        ls.run();
        list.remove();
    } else {
        ls.create();
    }
}
Run Code Online (Sandbox Code Playgroud)

谢谢

java exception concurrentmodification

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

java.util.ConcurrentModificationException是一个大问题,在这种情况下

当我注意到我在打电话时随机获得Exception时,我正在为我的决赛制作一个游戏 repaint();

这一切的原因是不同的对象在不同的​​时间更新,并有自己Thread更新自己.有时,实体会在绘制时自行更新.绘制线程以大约60-100 FPS运行,而实体大约每300毫秒更新一次.

游戏没有停止或有任何其他与此相关的问题.

我知道忽略它是一个坏主意,但还有什么我可以做的吗?拥有多个线程是我能想到的让游戏运行的唯一解决方案.

谢谢阅读!

编辑:当我在绘画时尝试删除实体时,问题似乎发生了; 两者都使用相同的List.

java concurrentmodification

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

编译时间检查或java.util.ConcurrentModificationException

下面的循环抛出ConcurrentModificationException.它是否应该给出编译器错误.运行时异常的原因是什么?

final List<String> list = new ArrayList<String>();
list.add("AAAAAAAAAAAAA");
for (final String it : list) {
    System.out.println(it);
    list.add("SSSSSSSSSS");
}
Run Code Online (Sandbox Code Playgroud)

java concurrency loops concurrentmodification

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

迭代列表时运行java.util.ConcurrentModificationException

我正在尝试将记录列表拆分为记录的子列表.我成功地将列表拆分为子列表,我想看到子列表的内容,但不知怎的,我继续遇到这个ConcurrentModificationException.

我的拆分方法:

/**
     * @param list - list of results
     * @param size - how many sublists
     * @return ret - returns a list containing the sublists
     * */
    public static <T> List<List<T>> split(List<T> list, int size) throws NullPointerException, IllegalArgumentException {
        if (list == null) {
            throw new NullPointerException("The list parameter is null.");
        }
        if (size <= 0) {
            throw new IllegalArgumentException("The size parameter must be more than 0.");
        }

        int recordsPerSubList = list.size() / size; // how many records …
Run Code Online (Sandbox Code Playgroud)

java list concurrentmodification

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

TryCatch ConcurrentModificationException捕获`30%的时间

我正在使用迭代器从列表中移除一个射弹,如果它超出了我的JPanel的边界.在使用迭代器之前它不会工作,但是只要我将方法放入try-catch中,迭代器就可以工作ConcurrentModificationException.代码现在可以工作,并且成功地从列表中移除了射弹,但是大约30%的时间,捕获命中并导致我的程序中出现断断续续的情况.我不知道为什么它只是偶尔捕获,但在有限的时间内,我的教授能够看到它,他认为这可能是一个同步问题.

这是运行我的程序的循环:

private void executeGameLoop() 
        {

            long nextFrameStart = System.nanoTime();
            while(panel.getRunning()) 
            {
                do 
                {
                    panel.repaint();
                    nextFrameStart += FRAME_PERIOD;
                } while(nextFrameStart < System.nanoTime());

                long remaining = nextFrameStart - System.nanoTime();
                panel.update();

                if (remaining > 0) 
                {
                    try 
                    {
                        Thread.sleep(remaining / 1000000);
                    } 
                    catch(Throwable e) 
                    {
                        System.out.println(e.getMessage()); 
                    }
                }
            }
        }
Run Code Online (Sandbox Code Playgroud)

这由循环at调用panel.update.它现在处理弹丸的更新:

public void update()
        {       
            randX = (int)((Math.random() * getWidth()) - (int)(Math.random() * getWidth()));
            randY = (int)((Math.random() * getHeight()) - (int)(Math.random() * getHeight()));

            int sizeX;
            int sizeY;

            try …
Run Code Online (Sandbox Code Playgroud)

java swing try-catch concurrentmodification

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

迭代ArrayList时出现Java ConcurrentModificationException

ConcurrentModificationException在迭代ArrayList并将对象添加到辅助节点时得到ArrayList.我真的不知道为什么,因为我没有编辑我正在迭代的列表.

这发生在我的代码的两个部分.这些是代码.

编辑 -代码1:

public static ConcurrentHashMap<Long, ArrayList<HistoricalIndex>> historicalIndexesMap = new ConcurrentHashMap<Long, ArrayList<HistoricalIndex>>();

ArrayList<HistoricalIndex> historicalIndexList = IndexService.historicalIndexesMap.get(id);
List<Double> tmpList = new ArrayList<Double>();
for(HistoricalIndex hi : historicalIndexList){ //EXCEPTION HERE
    if((System.currentTimeMillis()-hi.getTimestamp()) >= ONE_MINUTE){
        tmpList.add(hi.getIndex());
    }
}
Run Code Online (Sandbox Code Playgroud)

在上面的代码1中,我应该像这样复制historicalIndexList:

ArrayList<HistoricalIndex> historicalIndexList = new ArrayList<HistoricalIndex>(IndexService.historicalIndexesMap.get(id));
Run Code Online (Sandbox Code Playgroud)

而不是这样做:?

ArrayList<HistoricalIndex> historicalIndexList = IndexService.historicalIndexesMap.get(id);
Run Code Online (Sandbox Code Playgroud)

代码2:

List<Double> tmpList = new ArrayList<Double>();
for(HistoricalIndex hi : list){ //EXCEPTION HERE
    tmpList.add(hi.getIndex());
}
Run Code Online (Sandbox Code Playgroud)

有没有人知道为什么会这样?

堆栈跟踪:

21:19:50,426 ERROR [stderr] (pool-9-thread-6) java.util.ConcurrentModificationException
21:19:50,429 ERROR [stderr] (pool-9-thread-6)   at java.util.ArrayList$Itr.checkForComodification(ArrayList.java:859)
21:19:50,432 …
Run Code Online (Sandbox Code Playgroud)

java arraylist concurrentmodification

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