Java的" ConcurrentModificationException的"迭代。接下来,当运行时错误() [英] Java "ConcurrentModificationException" runtime error when iterating .next()

查看:164
本文介绍了Java的" ConcurrentModificationException的"迭代。接下来,当运行时错误()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据运行时错误消息在下面一行出现异常;

According to the runtime error message the Exception occurs in the following line;

VirusData v = iteratorVirusDB.next();


VirusData 是具有一个构造和包含有关每个数据库中,如病毒特定信息的一个重载的构造的类;


VirusData is a class with a constructor and an overloaded constructor containing specific information about each of the viruses in the database such as;


  • 字符串VNAME

  • 字符串vDefinition

与重载


  • 阵列与记号化定义(在xLength组分隔)

  • 阵列与LCS令牌

  • 浮动一个档次

iteratorVirusDB 类型< VirusData> VirusDB的.iterator() ,如以下所示的:

iteratorVirusDB of type <VirusData> is an .iterator() of VirusDB, as shown bellow:

Iterator<VirusData> iteratorVirusDB = virusDB.iterator();


VirusDB 是和类型的ArrayList &LT; VirusData&GT; 在哪里存放病毒对象的(名和DEF在这一点上)的,这样我可以在以后使用它们。


VirusDB is and ArrayList of type <VirusData> where I store the virus objects (name and def at this point) so that I can use them later.

ArrayList <VirusData> virusDB = new ArrayList<VirusData>();


和结束与,在该方法,它使用的所有的以上说明发生错误

private void selectDabataseMouseClicked(java.awt.event.MouseEvent evt) { while(iteratorVirusDB.hasNext()) { VirusData v = iteratorVirusDB.next(); //ERROR LINE String vSig = v.signature; v.tokens = tokenize.raw(vSig, true, tLength); ... } ... }




I could really do with some help and advice on how to approach this problem in order to get get the program to run successfully. Bellow, the full StackTrace:


我真的做了关于如何才能得到让程序运行成功解决这个问题的一些帮助和建议。贝娄,全堆栈跟踪:

run: Exception in thread "AWT-EventQueue-0" java.util.ConcurrentModificationException at java.util.AbstractList$Itr.checkForComodification(AbstractList.java:372) at java.util.AbstractList$Itr.next(AbstractList.java:343) at project_clean.Main.selectDabataseMouseClicked(Main.java:275) at project_clean.Main.access$100(Main.java:11) at project_clean.Main$2.mouseClicked(Main.java:76) at java.awt.AWTEventMulticaster.mouseClicked(AWTEventMulticaster.java:253) at java.awt.Component.processMouseEvent(Component.java:6270) at javax.swing.JComponent.processMouseEvent(JComponent.java:3267) at java.awt.Component.processEvent(Component.java:6032) at java.awt.Container.processEvent(Container.java:2041) at java.awt.Component.dispatchEventImpl(Component.java:4630) at java.awt.Container.dispatchEventImpl(Container.java:2099) at java.awt.Component.dispatchEvent(Component.java:4460) at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4577) at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4247) at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4168) at java.awt.Container.dispatchEventImpl(Container.java:2085) at java.awt.Window.dispatchEventImpl(Window.java:2478) at java.awt.Component.dispatchEvent(Component.java:4460) at java.awt.EventQueue.dispatchEvent(EventQueue.java:599) at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269) at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184) at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161) at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)


推荐答案

显而易见的解释是,你在调用之间修改 virusDB 。不得修改向量(除非通过的Iterator / 的ListIterator 方法),而使用迭代器遍历。

The obvious explanation is that you have modified the virusDB in between calls. You mustn't modify the vector (except through the Iterator / ListIterator methods) while iterating using an iterator.

code的这一片段将的总是的抛出一个 ConcurrentModificationException的

This snippet of code will always throw a ConcurrentModificationException:

import java.util.*;

class VirusData {
}

public class Test {

    public static void main(String[] args) {

        List<VirusData> list = new ArrayList<VirusData>() {{
            add(new VirusData());
            add(new VirusData());
            add(new VirusData());
        }};

        Iterator<VirusData> iterator = list.iterator();

        iterator.next();

        list.remove(0);
        VirusData s = iterator.next();
    }
}

从文件 ConcurrentModificationException的

例如,它不是一般允许的,而另一个线程上进行迭代一个线程修改集合。在一般情况下,迭代的结果在这些情况下不确定的。一些迭代器实现(包括那些由JRE提供的所有通用collection实现)可能选择如果检测到这种行为,抛出此异常。迭代器为此被称为快速失败迭代器,因为他们很快就完全失败,而在那在将来不确定的时间任意冒险,不确定性的行为。

请注意,此异常不会始终指出对象已经由不同线程并发修改。 如果一个线程发出的方法调用序列违反对象的合同,该对象可能抛出此异常。例如,如果一个线程直接修改集合,而它遍历集合,快速失败迭代器,迭代器将抛出此异常。

Note that this exception does not always indicate that an object has been concurrently modified by a different thread. If a single thread issues a sequence of method invocations that violates the contract of an object, the object may throw this exception. For example, if a thread modifies a collection directly while it is iterating over the collection with a fail-fast iterator, the iterator will throw this exception.

如果您的目的是在整个数据库中的每个方法被调用时迭代,我建议你做

If your intention is to iterate through the entire database each time the method is called, I suggest you do

private void selectDabataseMouseClicked(java.awt.event.MouseEvent evt) {
    Iterator<VirusData> iteratorVirusDB = virusDB.iterator();
    while(iteratorVirusDB.hasNext()) {
        VirusData v = iteratorVirusDB.next();
        String vSig = v.signature;                              

这篇关于Java的&QUOT; ConcurrentModificationException的&QUOT;迭代。接下来,当运行时错误()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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