0

If different threads work on different keys in the map, I think I don't need to consider synchronization. Is this correct?

For example, my map (an ordinary HashMap) has . One thread is only interested in k1 and might add/update/remove/retrieve K1's value, and the other thread is only interested in K2 and might add/update/remove/retrieve k2's value.

user3014901
  • 367
  • 2
  • 4
  • 15
  • No it is not correct. You will need synchronization around both `get()` calls and any call that can update or clear the map, all synchronizing on the same thing. You are correct if and only if there are no concurrent updaters. – user207421 Dec 14 '16 at 01:20
  • 2
    [Is a HashMap thread-safe for different keys?](http://stackoverflow.com/questions/2688629/is-a-hashmap-thread-safe-for-different-keys) – nail fei Dec 14 '16 at 01:20

0 Answers0