簡體   English   中英

ConcurrentModificationException的可能原因

[英]Possible reason for ConcurrentModificationException

我知道什么是ConcurrentModificationException。 我以前有過這些東西,之前也有解決過的東西,但現在有了Iterator可以解決。 但是,在這種情況下,我不明白為什么要拋出它。

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;
}

這是blob中的edgeHit方法:

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;

}

更新:

cornerVectors是一個列表視圖:

 List<PVector> cornerVectors;

它設置為:

list.subList(fromIndex, toIndex);

沒有其他威脅在運行。

這是堆棧跟蹤:

nl.doekewartena.contour.scanner處java.util.SubList.size(AbstractList.java:625)處的java.util.SubList.checkForComodification(AbstractList.java:752)處的線程“ Animation Thread”中的異常。 .nl.doekewartena.contour.scanner.BlobData.pointOnEdgeBlob(BlobData.java:333)的.Blob.edgeHit(Blob.java:229)在nl.doekewartena.contour.scanner.ContourFinder.scan(ContourFinder.java:555)處nl.doekewartena.contour.scanner.ContourFinder.scan(ContourFinder.java:469)位於exclude.T04_ContourFinder.draw(T04_ContourFinder.java:38)在processing.core.PApplet.handleDraw(PApplet.java:2386)在processing.core .PGraphicsJava2D.requestDraw(PGraphicsJava2D.java:240)位於java.lang.Thread.run(Thread.java:695)的processing.core.PApplet.run(PApplet.java:2256)

一旦你做

x = list.subList(fromIndex, toIndex);

該列表不應修改,否則在訪問x時將拋出CME

從.subList javadocs:

如果后備列表(即此列表)以結構方式(而不是通過返回的列表)進行了修改,則此方法返回的列表的語義將變得不確定。 (結構修改是指更改此列表的大小的結構修改,或者以其他方式干擾此列表的方式,即正在進行的迭代可能會產生錯誤的結果。)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM