Concurrent updates to non-synchronized HashMap
can obviously cause a livelock or other data corruptions; to avoid this, one should use the concurrent version or implement a synchronization mechanism.
Can concurrent calls to HashMap.get() change the state of the HashMap, like rehashing?
Update:
Some commenters wondered about the practical aspects of the question, besides the theoretical joy of nitpicking data structure behavior.
If get()
does not change the state of the HashMap
(and can't cause a livelock for another reaseon), than a single thread can create a HashMap in advance, and then many threads can read from it concurrently. If concurrent reading isn't safe, we need ConcurrentHashMap
for every multi-threaded access, regardless of type.