D
Daniel
Hi,
I have a situation where I first popuplate a Collection (HashMap,
TreeMap, and friends) with data. After the Collection has been fully
populated, it will be accessed concurrently by many threads, but only
for read operations - specifically, get() and iterator(). My question
is this: do I need to synchronize access to the maps, since the data
will NEVER be structurally modified after the initialization?
My understanding is that I do not have to. To quote a piece of the
HashMap API documentation:
"If multiple threads access this map concurrently, and at least one of
the threads modifies the map structurally, it must be synchronized
externally. (A structural modification is any operation that adds or
deletes one or more mappings; merely changing the value associated
with a key that an instance already contains is not a structural
modification.)"
I read this as a guarantee that if the map is not structurally
modified, then you do not need to synchronize it. However I have seen
other places on the Java website and others that have blanket
statements such as "ALL access to a HashMap must be synchronized if it
is accessed by multiple threads." Is it safe to assume they're talking
about the case where the map might be modified by one of these
threads?
Thanks for any pointers or assurances,
Daniel
P.S. I find it interesting that the documentation for HashMap implies
that you do not need to synchronize access even when changing a value
associated with a key. Is the value assignment atomic?
I have a situation where I first popuplate a Collection (HashMap,
TreeMap, and friends) with data. After the Collection has been fully
populated, it will be accessed concurrently by many threads, but only
for read operations - specifically, get() and iterator(). My question
is this: do I need to synchronize access to the maps, since the data
will NEVER be structurally modified after the initialization?
My understanding is that I do not have to. To quote a piece of the
HashMap API documentation:
"If multiple threads access this map concurrently, and at least one of
the threads modifies the map structurally, it must be synchronized
externally. (A structural modification is any operation that adds or
deletes one or more mappings; merely changing the value associated
with a key that an instance already contains is not a structural
modification.)"
I read this as a guarantee that if the map is not structurally
modified, then you do not need to synchronize it. However I have seen
other places on the Java website and others that have blanket
statements such as "ALL access to a HashMap must be synchronized if it
is accessed by multiple threads." Is it safe to assume they're talking
about the case where the map might be modified by one of these
threads?
Thanks for any pointers or assurances,
Daniel
P.S. I find it interesting that the documentation for HashMap implies
that you do not need to synchronize access even when changing a value
associated with a key. Is the value assignment atomic?