Does ConcurrentHashMap need synchronization when incrementing its values?
问题 I know ConcurrentHashMap is thread-safe e.g.putIfAbsent,Replace etc., but I was wondering, is a block of code like the one below safe? if (accumulator.containsKey(key)) { //accumulator is a ConcurrentHashMap accumulator.put(key, accumulator.get(key)+1); } else { accumulator.put(key, 0); } Keep in mind that the accumulator value for a key may be asked by two different threads simultaneously, which would cause a problem in a normal HashMap. So do I need something like this? ConcurrentHashMap