The following piece of code throws a ConcurrentModificationException almost every time it is called. The second piece of code does not throw the exception, however it is not the correct logic that I need. If the object is an instance of EditorFrame
, I need to invoke a custom disposal strategy which is what the close()
method is. However if it is just a basic frame I want it to call dispose()
.
I've looked around this site and followed some instructions, but none that I found have worked.
The code that throws the exception:
synchronized (frameList) {
for (Iterator<JFrame> it = frameList.iterator(); it.hasNext();) {
JFrame frame = it.next();
if (frame instanceof EditorFrame) ((EditorFrame) frame).close();
else frame.dispose();
it.remove();
}
}
This code works, but it's not what I want:
synchronized (frameList) {
for (Iterator<JFrame> it = frameList.iterator(); it.hasNext();) {
JFrame frame = it.next();
frame.dispose();
it.remove();
}
}
Thanks for helping!