Uses for each syntax using Collections.synchronizedSet

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(); // Must be in the synchronized block while (i.hasNext()) foo(i.next()); } 

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.

+4
source share
3 answers
  • should I wrap each cycle of the cycle ... No, you do not need to wrap each cycle of the cycle with a synchronized block, infact, you should not even do this, because it comes with a penalty for execution. You need to wrap only those for loops that iterate over collections that can be processed simultaneously by multiple threads.

  • my IDE (IntelliJ IDEA) is reporting .... In your particular case, this is not a problem, because if different threads have different instances of s, then obviously the iterators will also be different.

+3
source

The documentation clearly states that you must do this in the face of concurrent access. This is also a good reason why you should do this:

In the face of concurrent access, it is imperative that the user manually synchronizes the returned collection when iterating over it. The reason is that iteration is performed through several calls in the collection, which must be combined into one atomic operation. The following is an idiom for iterating over a collection associated with a shell.

http://java.sun.com/docs/books/tutorial/collections/implementations/wrapper.html

To learn more about the "final variable in the synchronized block", look at this: The final variable and the synchronized block in java

+3
source

Since each of them uses an Iterator under the covers, you still ConcurrentModificationException during handling a for each if your Set structurally modified.

Regarding the warning that your IDE provides, if you reassign your s , there is a chance that different threads see different objects when trying to synchronize to s . Using volatile should help here.

+1
source

Source: https://habr.com/ru/post/1301299/


All Articles