I used synchronized list, and i still get ConcurrentModificationException
java, multithreading, synchronized, vector
Solution
You cannot modify a Vector while iterating over it. Store the items to add in a separate vector, and move them to the Vector when the loop is finished or loop over a copy of the original Vector.
ADDED: To get a mutex around the Vector in java, do this in both functions:
synchronized (list) {
// modifying list
}
and:
synchronized (list) {
// iterating over list
}
Of course I've assumed that the list is named `list`
Problem
I'm using `Vector` instead of `ArrayList` to make a list safe in multi-threaded enviroment. But I keep getting `ConcurrentModificationException` when I trying to add items to the `Vector` while iterating it. Why is that and how can I prevent it?