I have CHM as below. I call a method setDataProcessfrom one background thread whenever there is any update. And I always call getDataMappingfrom several streams of readers.
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: will any change in mappingsHolderCHM be visible to all reader threads instantly, or do I need to use volatile for this?
source
share