The problem is this. I made a set
Set<User> users = Collections.synchronizedSet(new HashSet<User>()) ... for(User u : users){ //do something with u }
Now, according to the Java documentation.
It is imperative that the user synchronizes manually the returned sorted set when iterating over it or any of its subsets, headSet or tailSet views.
SortedSet s = Collections.synchronizedSortedSet(new HashSortedSet()); ... synchronized(s) { Iterator i = s.iterator();
I'm sure iterators are used for each syntax, but I'm not sure if I should wrap each of them for each loop in a synchronized block.
Another thing is that my IDE (IntelliJ IDEA) reports that using a synchronized block over a field that is not final is unlikely to have useful semantics, since different threads can be different objects, even when working on the same object.
source share