并发修改执行 [英] ConcurrentModificationExecption

查看:51
本文介绍了并发修改执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个正常的数据库调用,该调用从数据库中收集信息.我使用这些信息来创建我的对象( CallQueue ),然后将这些对象添加到列表中,然后返回该列表.

I have a normal Database call that gathers information from my database. i use these informations to create my objects (CallQueue) these objects are then added to a list and the list is then returned.

突然,我发现我的原始代码未按预期工作,因为我创建了重复项,所以现在我试图使所有重复项都无法创建!但是有问题!

Suddenly i discovered that my originally code did not work as intended because i created dublicates and so now i am trying to void that any dublicates are being created! But there is a problem!

我无法遍历列表并检查是否已创建对象!

I am unable to loop through my list and check wether or not the object is already created!

这是我的代码:

while (query.next()) {
    if (!queues.isEmpty()) {
        /*This gives the Execption->*/
        for (CallQueue callQueue : queues) {
            if (callQueue.getType().equals(query.getString("KØ"))) {
                double decimalTime = query.getDouble("TID");
                int hourOfDay = (int)Math.round(24 * decimalTime);
                int callAmount = query.getInteger("ANTAL_KALD");
                if (hourOfDay > 19) {
                    hourOfDay = 19;
                }
                callQueue.addCallsByTime(hourOfDay, callAmount);
            } else {
                String queueName = query.getString("Kø");
                if (!queueName.equalsIgnoreCase("PrivatOverflow")) {
                    CallQueue cq = new CallQueue(query.getString("KØ"));
                    double decimalTime = query.getDouble("TID");
                    int hourOfDay = (int)Math.round(24 * decimalTime); 
                    int callAmount = query.getInteger("ANTAL_KALD");
                    if (hourOfDay > 19) {
                        hourOfDay = 19;
                    }
                    cq.addCallsByTime(hourOfDay, callAmount);
                    queues.add(cq);
                }
            }
        }
    } else {
        String queueName = query.getString("Kø");
        if (!queueName.equalsIgnoreCase("PrivatOverflow")) {
            CallQueue cq = new CallQueue(query.getString("KØ"));
            double decimalTime = query.getDouble("TID");
            int hourOfDay = (int)Math.round(24 * decimalTime); 
            int callAmount = query.getInteger("ANTAL_KALD");
            if (hourOfDay > 19) {
                hourOfDay = 19;
            }
            cq.addCallsByTime(hourOfDay, callAmount);
            queues.add(cq);
        }
    }
}

for (CallQueue callQueue : queues) {
    System.out.println(callQueue.getType());
}
query.Close();
return queues;

我从中得到的执行力是:

The execption i get from this is:

Caused by: java.util.ConcurrentModificationException

ive试图在ConcurrentModificationException

有人可以帮助我解决此问题吗?

Can anyone help me fix this problem?

推荐答案

您正在迭代中进行添加.根据规范,不允许您修改要迭代的集合.

You're doing an add inside of your iteration. As per the spec, you're not allowed to modify the collection you're iterating over.

经典的解决方案是首先创建集合的副本,然后对其进行迭代.另一种解决方案是不使用迭代器(简短的foreach表示法是隐式使用它),而是使用索引手动进行迭代.

The classic solution is to make a copy of the collection first and iterate over that instead. Another solution is to not use the iterator (the short foreach notation is using it implicitly) but to manually iterate using an index.

for (int i=0; i<queues.size(); i++) {
    CallQueue callQueue = queues.get(i);

    ... code goes here

}

更好的解决方案是使用Set而不是列表(除非顺序对您很重要).那确实意味着您将必须正确实现equals和hashcode.

A better solution would be to use a Set instead of a list (unless the order is important for you). That does mean you would have to implement equals and hashcode properly.

顺便说一句:我相信您的代码有缺陷.您正在遍历列表,如果遇到的项目不匹配,则在末尾添加一个.这意味着,如果您要查找的项目是列表中的第x个项目,则您将添加x次新项目.我严重怀疑那是您所需要的.如果您进行一些重构,这将立即变得很清楚.

这篇关于并发修改执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆