Hard👤 8-15 years 1 min read

Why can a plain HashMap corrupt data — or even spin the CPU — under concurrent access?

Asked inAmazonMicrosoftDeloitte
#hashmap#race condition#infinite loop#thread-safety#production
Report issue

⚡ Short Answer

HashMap isn't thread-safe. Concurrent puts during a resize can lose entries or, in Java 7's linked-list transfer, create a cycle that makes get() spin forever (100% CPU). Java 8 fixed the infinite loop but concurrent use still corrupts data. Use ConcurrentHashMap.

Coffee Chat Question

Concept Made Simple

Why can a plain HashMap corrupt data — or even spin the CPU — under concurrent access?

🧠Mind Map Answer

Remember It Faster

Java 7resize could create a linked-list cycle → infinite loop
Java 8no loop, but lost updates / NPE / wrong size
FixConcurrentHashMap

🔥What If?

Think Beyond the Expected

A production thread is pegged at 100% CPU inside HashMap.get() — what happened?

Classic Java 7 symptom: two threads resized a shared HashMap concurrently, the linked-list transfer formed a cycle, and now get() traverses that cycle forever. The real fix isn't a thread dump — it's replacing the shared HashMap with ConcurrentHashMap.

😂Real World

A pegged-CPU incident traced to HashMap.get() in a thread dump is a notorious real-world bug; the root cause is a HashMap shared across threads without synchronization.

🎯Interviewer's Expectation

Keywords they're listening for:

not thread-saferesize raceJava 7 infinite loopJava 8 data corruptionConcurrentHashMap fix

⚠️Common Mistakes

  • Sharing a HashMap across threads
  • Assuming synchronizedMap makes compound ops atomic
  • Treating it as a JVM bug rather than misuse

Best Practices

  • Use ConcurrentHashMap for shared maps
  • Never share a plain HashMap across threads
  • Guard compound operations with atomic methods

🔁Follow-up Questions

  • 1Why is Collections.synchronizedMap not always enough?
  • 2How does ConcurrentHashMap avoid this?
  • 3How would you detect this from a thread dump?

🧩Related Technologies

ConcurrentHashMapCollections.synchronizedMapjstack

Continue Learning with AI

Take this question deeper with your favourite AI assistant. Pick a depth, copy the prompt, or open it directly — AI is your learning companion, not a shortcut.

Plain-language foundations

I'm preparing for a software engineering interview and want to understand this from scratch, as a beginner.

Topic: ConcurrentHashMap (Java Collections)
Interview question: "Why can a plain HashMap corrupt data — or even spin the CPU — under concurrent access?"

Please:
1. Explain the core idea in simple, plain language, using an everyday analogy.
2. Define any technical terms you use.
3. Walk through one small, concrete example.
4. Finish with a single sentence I can easily remember.

Keep the tone friendly and assume I'm new to this topic.
Open inChatGPTGeminiClaude

Was this answer helpful?

Support our platform by exploring our recommended products.

As an Amazon affiliate, purchases through these links may earn us a small commission — at no extra cost to you. It helps keep Full Stack Interview Guru free.

Related Questions