I have a CHM defined as below. I am calling setDataProcess
method from a single background thread whenever there is any update. And I am calling getDataMapping
from multiple reader threads always.
private static final ConcurrentHashMap<ProcessType, Mapping> mappingsHolder = new ConcurrentHashMap<ProcessType, Mapping>();
private static final CountDownLatch hasInitialized = new CountDownLatch(ProcessType.values().length);
public static void setDataProcess(ProcessType processType, Mapping newMapData) {
mappingsHolder.put(processType, newMapData);
hasInitialized.countDown();
}
public static Mapping getDataMapping(ProcessType processType) {
try {
hasInitialized.await();
return mappingsHolder.get(processType);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new IllegalStateException(e);
}
}
Question is - Any change in mappingsHolder
CHM will be visible to all the reader threads instantly or I need to use volatile for that?