2014-12-03 63 views
0

我知道什麼是ConcurrentModificationException。我之前有過它們,我之前解決過它們,我可以用迭代器逃脫。 但是,在這種情況下,我不明白爲什麼它被拋出。ConcurrentModificationException的可能原因

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

這裏是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); 

沒有其他威脅在運行。

這裏是堆棧跟蹤:

在 java.util.SubList.checkForComodification(AbstractList.java:752)異常在線程 「動畫線程」 java.util.ConcurrentModificationException在 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 )在在 java.lang.Thread.run(Thread.java:695 processing.core.PGraphicsJava2D.requestDraw(PGraphicsJava2D.java:240) 在processing.core.PApplet.run(PApplet.java:2256))

+2

那麼,什麼是'cornerVectors'? – 2014-12-03 02:30:45

+1

而其他線程沒有活動? – 2014-12-03 02:32:48

+0

我相當肯定有些事情你沒有告訴我們。 – 2014-12-03 02:50:12

回答

1

一旦你這樣做

x = list.subList(fromIndex, toIndex); 

列表中不應該被修改或accesing X

當從.subList javadocs中它會拋出CME:

通過該方法變得不確定返回的列表中的語義是否 背襯列表(即,該列表)在結構上在改性除了通過返回列表之外的任何方式 。 (結構上修改是指更改此列表的大小,或者以其他方式干擾它以這樣一種方式 在正在進行的迭代產生不正確的結果 。)

相關問題