🎙 Free live masterclass · Sun, 29 Nov, 7:00 pm IST · Java Full Stack roadmap Register free →
Skills101
← Quiz: Java Concurrency Utilities: all 10 questions

Quiz: Java Concurrency Utilities · Question 4 of 10

Which call counts a word in a ConcurrentHashMap safely from many threads?

Which call counts a word in a ConcurrentHashMap safely from many threads?

  1. A.map.put(w, map.get(w) + 1)
  2. B.if (map.containsKey(w)) map.put(w, map.get(w) + 1)
  3. C.map.merge(w, 1, Integer::sum)
  4. D.map.putAll(Map.of(w, 1))
Answer & explanation

Correct answer: map.merge(w, 1, Integer::sum)

merge performs the read-modify-write as one atomic step; the other options are separate reads and writes.

Can you get the rest right without peeking?

Take the timed Quiz: Java Concurrency Utilities quiz free. Pass it to earn XP and count towards your certificate.

Start the quiz →