Skip to main content
Journey Uncommon Logo
JourneyUncommon
mediumJavaConcurrentHashMapSingle-choice MCQ

Two threads run this code concurrently with an initially empty ConcurrentHashMap. Why can the final count be wrong?

ConcurrentHashMap<String,Integer> m = new ConcurrentHashMap<>(); // each of many threads runs: Integer v = m.get("k"); m.put("k", v == null ? 1 : v + 1);