I have a singleton map used throughout which means I cannot simply reassign the entire map. I control the map so I can make it a ConcurrentHashMap.
I am populating it in a separate thread (I am using Quartz to create a scheduled job). I need to remove all of the values from the map and them put a bunch of new values in. I am concerned, though, that there will be CPU cycles where the map is empty where a separate thread might read the map. I do this population of the map every N seconds.
If I wasn't concerned about making the operation atomic, I could just do:
Map<String, String> newMap = ...;
myMap.clear();
// What if a read happens here?
myMap.putAll(newMap);
I don't know very much about synchronization, but I don't think putting this in a synchronized block will help, since it is not this code that is doing the reading.
ConcurrentHashMap provides several useful atomic operations, but it doesn't look like it provides any that help me with this problem.