Is it effective to control synchronization map using synchronized (map)?

Viewed 32

I create a map to store the value,but I find that sometimes the value is wrong with multi-thread concurrency problem.So I wonder if the GC is happen,do the synchronized (map) code can only allow one thread to run?I find the after gc,the map's hashcode may be changed.

private static Map<String, Map<String,String>> instanceMap = new ConcurrentHashMap<>();
//some thread to put key to instanceMap
//some thread to checkMapState
private void checkMapState(String key){
    Map localMap=instanceMap.get(key);
    synchronized (localMap) {
        if (localMap.containsKey(key)) {
            //code to clear map by key
            //log the same key not only one time
            log.info("key:{} have enter synchronized code",key);
        }
    }
}
0 Answers
Related