LearnFrenzy
Sign in Take a free test

Concurrency and Executors · Concurrency and Executors MCQ · Question 14

Atomic updates on a ConcurrentHashMap Answer, explanation & discussion

  • MCQ
  • Medium
  • 0 comments
  • Answer explained
  • Moderated community
Question 14 Back to Exercise

Why is map.merge(word, 1, Integer::sum) safe on a ConcurrentHashMap while map.put(word, map.get(word) + 1) is not?

  • A merge is faster
  • B merge performs the read-modify-write atomically for that key; get-then-put can interleave with other threads and lose updates Correct
  • C put is not allowed
  • D There is no difference
Correct Answer: Option B

compute and putIfAbsent are also atomic.

Discussion & Comments 0

Share your method, a shortcut, or ask about a step you didn't follow.

No comments yet. Be the first to share your solution!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links