I wanted to avoid syncing so I tried to use iterators. The only place where I modify array looks as follows:
if (lastSegment.trackpoints.size() > maxPoints)
{
ListIterator<TrackPoint> points = lastSegment.trackpoints.listIterator();
points.next();
points.remove();
}
ListIterator<TrackPoint> points = lastSegment.trackpoints.listIterator(lastSegment.trackpoints.size());
points.add(lastTrackPoint);
And array traversal looks as follows:
for (Iterator<Track.TrackSegment> segments = track.getSegments().iterator(); segments.hasNext();)
{
Track.TrackSegment segment = segments.next();
for (Iterator<Track.TrackPoint> points = segment.getPoints().iterator(); points.hasNext();)
{
Track.TrackPoint tp = points.next();
// ^^^ HERE I GET ConcurentModificationException
// =============================================
...
}
}
So, what's wrong with iterators? Second level arrays are huge, so I do not want to copy them nor I want to rely on synchronization outside of my Track
class.