Reputation: 623
I caught an exception with multi thread programing.
I saw it at first, but I don't know how to solve it.
then I search about ConcurrentModificationException then it says
ConcurrentModificationException should be used only to detect bugs
What does it mean? Maybe it says "you should only catch this exception" doesn't it?? or not??
Should I solve it??
Upvotes: 1
Views: 278
Reputation: 26882
You should never catch this exception. You must find out why the exception is thrown and fix the code. Note also that this exception won't be thrown every time. So if you test once and the exception is gone, you could still have problems. The best way to cope with this is to write good concurrent test cases. This still isn't bullet proof but it's the best thing you can.
To summarize, yes you have to solve the exception, and doing that properly canbe quite challenging. If you are dealing with production code, I recommend getting help with your code.
Upvotes: 0
Reputation: 2109
ConcurrentModificationException
often occur when you're modifying a collection while you are iterating over its elements.
Yes, you need to locate the bug and resolve the issue.
Try to find out if you are modifying while iterating or any other thread is involved in altering the collection. You need to figure out.
Could you post your code please?
Upvotes: 0
Reputation: 500257
The Javadoc explains the circumstances that typically lead to this exception:
This exception may be thrown by methods that have detected concurrent modification of an object when such modification is not permissible.
For example, it is not generally permssible for one thread to modify a Collection while another thread is iterating over it. In general, the results of the iteration are undefined under these circumstances. Some Iterator implementations (including those of all the collection implementations provided by the JRE) may choose to throw this exception if this behavior is detected. Iterators that do this are known as fail-fast iterators, as they fail quickly and cleanly, rather that risking arbitrary, non-deterministic behavior at an undetermined time in the future.
In that document, "ConcurrentModificationException should be used only to detect bugs" means that you should not catch it in your code and try to recover. You should track down the bug that's causing it, and fix the bug.
Upvotes: 2