Are ConcurrentHashMap changes visible for all threads?

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?

+4
source share
3 answers

, CountDownLatch , , . , -, ProcessType. CountDownLatch , , javadocs :

: , , countDown() await() .

CountDownLatch, ConcurrentHashMap , , .

( get) , ( put remove). Retrievals , .

, , , , . .

+4

CountdownLatch , , countDown, , , Manouti.

ConcurrentHashMap, api, :

( ) , .

.

+2

(.. ) , , volatile . , , .

, , : , . , concurrentHashMap , . , . - , .

0

All Articles