Exception in thread "Thread-7" java.util.ConcurrentModificationException
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.
I want my playground back. Here, I'll give you this tiny ad for it:
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your databasehttps://coderanch.com/t/704633/RavenDB-Open-Source-NoSQL-Database