Possible reason for ConcurrentModificationException

感情迁移 提交于 2019-12-11 14:52:47

问题


I know what a ConcurrentModificationException is. I had them before, I solved them before and I can get away with a Iterator. However, In this case I don't understand why it's being thrown.

public boolean pointOnEdgeBlob(int x, int y, float edgeHitEpsilon) {

    init();

    for (int i = 0; i < nOfBlobs; i++) {
        Blob b = blobs.get(i);
         // >>>>>>>>>>>>>>>>>>>>> here it calls the method where it goes wrong
        if (b.edgeHit(x, y, edgeHitEpsilon)) return true; 
    }
    return false;
}

Here is the edgeHit method that it in the blob:

public boolean edgeHit(float x, float y, float edgeHitEpsilon) {


    // quick test if it's worth it
    if (x < getMinX()-edgeHitEpsilon || x > getMaxX()+edgeHitEpsilon || y < getMinY()-edgeHitEpsilon || y > getMaxY()+edgeHitEpsilon) {
        return false;
    }

    // the last one should be connected to the first
    // >>>>>>>>>>>>>>> if i comment the part in the for loop then this get's the problem.
    PVector pre = cornerVectors.get(cornerVectors.size() -1);
    PVector cur;

    for (int i = 0; i < cornerVectors.size(); i++) {
        // >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> on this line it throws
        cur = cornerVectors.get(i); 

        if(Blob.onLine(pre, cur, x, y, edgeHitEpsilon)) {
            return true;
        }

        pre = cur;

    }

    return false;

}

Update:

cornerVectors is a list view:

 List<PVector> cornerVectors;

It get's set with:

list.subList(fromIndex, toIndex);

There are no other threats running.

Here is the stack trace:

Exception in thread "Animation Thread" java.util.ConcurrentModificationException at java.util.SubList.checkForComodification(AbstractList.java:752) at java.util.SubList.size(AbstractList.java:625) at nl.doekewartena.contour.scanner.Blob.edgeHit(Blob.java:229) at nl.doekewartena.contour.scanner.BlobData.pointOnEdgeBlob(BlobData.java:333) at nl.doekewartena.contour.scanner.ContourFinder.scan(ContourFinder.java:555) at nl.doekewartena.contour.scanner.ContourFinder.scan(ContourFinder.java:469) at exclude.T04_ContourFinder.draw(T04_ContourFinder.java:38) at processing.core.PApplet.handleDraw(PApplet.java:2386) at processing.core.PGraphicsJava2D.requestDraw(PGraphicsJava2D.java:240) at processing.core.PApplet.run(PApplet.java:2256) at java.lang.Thread.run(Thread.java:695)


回答1:


Once you do

x = list.subList(fromIndex, toIndex);

the list should not be modified or it will throw CME when accesing x

From the .subList javadocs:

The semantics of the list returned by this method become undefined if the backing list (i.e., this list) is structurally modified in any way other than via the returned list. (Structural modifications are those that change the size of this list, or otherwise perturb it in such a fashion that iterations in progress may yield incorrect results.)



来源:https://stackoverflow.com/questions/27262602/possible-reason-for-concurrentmodificationexception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!