I am not looking for differences among various data structures like HashMap
, Collections.synchronizedMap(Map)
or HashTable
. SO is full of such posts.
I also know that ConcurrentHashMap will not throw ConcurrentModificationException
for reader thread if another writer thread modifies the map.
What I am looking for about practical scenarios when a reader thread can afford to work on stale data?
i.e. lets say thread-1 gets an Iterator
at time T1 in a thread then at later time T2 , an element is inserted to map by another thread thread-2. ConcurrentHashMap
says that thread-1 is not guaranteed to see updated data so I guess it keeps iterating over stale data.
Can anybody point me to concrete problems where this might be considered OK? like in implementing cache, multi threaded web services etc or problems from your own practical experience??
All the problems that I encountered always required updated view of data since that was the whole purpose of inserting data to a map so readers can work on updated data, alternatively, what were the problems where a synchronized map not a good fit?
In theory all looks good but my curiosity arose since I never encountered any such scenario in my day job where readers could afford to work on stale view.
In general - there are rare cases when you don't need exact data, and at the same time cannot afford it. An often cited example is number of "likes" a post has - no one cares whether it's 2564 or 2565, but the "+1" must be pretty fast.