I\'ve been at this for a week now doing my research on how to properly synchronize an ArrayList.
My main issue in a nutshell is I have a \"master\" ArrayList of objects.
If you're modifying during an iteration, yeah, you have to use option 3. None of the others will actually do what you want.
More specifically: given what you want to do, you have to lock the entire list for the length of the iteration, because you might modify it in the middle, which would corrupt any other iterators working on the list at the same time. That means option 3, since the Java language can't just have a "synchronized iterator" -- the iterator itself can only synchronize individual calls to hasNext()
or next()
, but it can't synchronize across the entire length of the iteration.