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!