java - Is changing the value of a Map an atomic operation? -


i wondering if synchronization or using concurrent class necessary, or conversely thread safe use non concurrent class , no synchronization on map in multi threaded environment, if modification map changing values of map.

the reason ask hashmap ( , other non concurrent maps documentation ) have comment:

note implementation not synchronized. if multiple threads access hash map concurrently, , @ least 1 of threads modifies map structurally, must synchronized externally. (a structural modification operation adds or deletes 1 or more mappings; merely changing value associated key instance contains not structural modification.) typically accomplished synchronizing on object naturally encapsulates map.

which makes me believe if modification not structural (i.e. there no added or deleted) should able update (non concurrent) map sans synchronization.

am reading correct? i.e. updating of value in map atomic process?

updating map value not atomic process. however, having multiple different threads each try modify map values concurrently not result in strange exceptions or errors due concurrency errors. example, won't cause 1 of key/value pairs disappear, or delete random elements out of map.

however, updates made 1 thread when updating key/value pair not visible other threads unless there other synchronization going on (for example, if values things atomicintegers). on top of this, there's no guarantee thread see own updates, since might clobbered other thread.

hope helps!


Comments

Popular posts from this blog

delphi - How to convert bitmaps to video? -

jasper reports - Fixed header in Excel using JasperReports -

python - ('The SQL contains 0 parameter markers, but 50 parameters were supplied', 'HY000') or TypeError: 'tuple' object is not callable -