Exception in thread "Thread-7" java.util.ConcurrentModificationException
at java.util.AbstractList$Itr.checkForComodification(AbstractList.java:449)
at java.util.AbstractList$Itr.next(AbstractList.java:420)
at Main.CGuiUpdater.run(CGuiUpdater.java:139)
at java.lang.Thread.run(Thread.java:595)
To be or not to be. It's a question.
"I'm not back." - Bill Harding, Twister
To be or not to be. It's a question.
"I'm not back." - Bill Harding, Twister
qunfeng wang wrote:
It's a static List.
Praveen Kumar M K wrote:Be wary of this - ConcurrentModificationException bug.
Not sure if its fixed in Java 7, however.
Note that fail-fast behavior cannot be guaranteed [snip]. Fail-fast operations throw ConcurrentModificationException on a best-effort basis. Therefore, it would be wrong to write a program that depended on this exception for its correctness
Praveen Kumar M K wrote:that too if one were using foreach loop instead of iterator methods.
Dennis Hopper wrote:I know this is an old topic but a simple, elegant solution is as follows:
Since Java does shallow copying on clone() calls this seems to work fine and since they (original & clone) have different iterators there's no violation when the original is modified.
I had a similar problem with one of the method variables. Spent hours debugging . Finally had to use Collections.unmodifiableList to
retrieve a unmodifiable list.
Feel it has more to do with the JRE being used.
Dennis Hopper wrote:
Since evidently it requires explanation I will explain. This isn't necessarily slower than a normal for-loop;
just make the clone call and save it to a variable.
And this code's applicability is not limited to clearing an array. Using this technique one can use all the functions of the List class and without the liability of having to use the ugly Iterator interface.
And since you aren't modifying the original
you don't need to worry about throwing ConcurrentModificationException instances. This should be pretty obviously the correct solution for this problem.
expectation is the root of all heartache - shakespeare. tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
|