Why did a HashMap frequency counter fail when multiple threads updated it at the same time?

Practice this question from Quiz: Java Concurrency, with options and an explanation.

Question

Why did a HashMap frequency counter fail when multiple threads updated it at the same time?

Option A — Correct answer

HashMap is not thread-safe and increments are compound operations

Concurrent read-modify-write operations can race.

Option B

HashMap only stores strings

Option C

HashMap is always sorted

Option D

HashMap prevents all writes

Explanation

HashMap is not thread-safe and count updates are compound read-modify-write operations that can race.

Quiz: Java Concurrency

All questions · Previous question · Next question

Open on QuizMaker