Using ConcurrentHashMap, when is synchronizing necessary?
I have a ConcurrentHashMap where I do the following: sequences = new ConcurrentHashMap<Class<?>, AtomicLong>(); if(!sequences.containsKey(table)) { synchronized (sequences) { if(!sequences.containsKey(table)) initializeHashMapKeyValue(table); } } My question is - is it unnecessary to make the extra if(!sequences.containsKey(table)) Check inside the synschronized block so other threads wont initialize the same hashmap value? Maybe the check is necessary and I am doing it wrong? It seems a bit silly what I'm doing, but I think it is necessary. All operations on a ConcurrentHashMap are thread