Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 751077
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T14:37:30+00:00 2026-05-14T14:37:30+00:00

If I have two multiple threads accessing a HashMap, but guarantee that they’ll never

  • 0

If I have two multiple threads accessing a HashMap, but guarantee that they’ll never be accessing the same key at the same time, could that still lead to a race condition?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T14:37:30+00:00Added an answer on May 14, 2026 at 2:37 pm

    In @dotsid’s answer he says this:

    If you change a HashMap in any way then your code is simply broken.

    He is correct. A HashMap is not thread-safe by design. If it is updated without proper synchronization it may break, even if the threads are using disjoint sets of keys1. Here are just some2 of the things that can go wrong.

    • If one thread does a put, then another thread may see a stale value for the hashmap’s size.

    • If one thread does a put with a key that is (currently) in the same hash bucket as the second thread’s key, second thread’s map entry might get lost, temporarily or permanently. (This could lead to a get unexpectedly returning null, or other behavior.) It depends on how the hash chains (or whatever) are implemented.

    • When a thread does a put that triggers a rebuild of the table, another thread may see transient or stale versions of the hashtable array reference, its size, its contents or the hash chains. Chaos may ensue.

    • When a thread does a put for a key that collides with some key used by some other thread, and the latter thread does a put for its key, then the latter might see a stale copy of hash chain reference. Chaos may ensue.

    • When one thread probes the table with a key that collides with one of some other thread’s keys, it may encounter that key on the chain. It will call equals on that key, and if the threads are not synchronized, the equals method may encounter stale state in that key.

    And if you have two threads simultaneously doing put or remove requests, there are numerous opportunities for race conditions.

    I can think of three solutions:

    • Use a ConcurrentHashMap.
    • Use a regular HashMap but use external synchronization using primitive mutexes, Lock objects, and so on. But beware that this can lead to a concurrency bottleneck due to lock contention.
    • Use a different HashMap for each thread. If the threads really have disjoint sets of keys, then there should be no need (from an algorithmic perspective) for them to share a single Map. Indeed, if your algorithms involve the threads iterating the keys, values or entries of the map at some point, splitting the single map into multiple maps could give a significant speedup for that part of the processing.

    1 – Even though the key sets are disjoint, keys from one set will at some point end up on the same hash chain as keys from another set. This is unavoidable.
    2 – We cannot enumerate all of the possible things that could go wrong. For a start, we can’t predict how all JVMs will handle the unspecified aspects of the JMM … on all platforms. But you should NOT be relying on that kind of information anyway. All you need to know is that it is fundamentally wrong to use a HashMap like this. An application that does this is broken … even if you haven’t observed the symptoms of the brokenness yet.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 496k
  • Answers 496k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I am surprised about your concerns on OpenSplice DDS adoption.… May 16, 2026 at 11:45 am
  • Editorial Team
    Editorial Team added an answer Take a look at MGTwitterEngine and Twitter + OAuth library May 16, 2026 at 11:45 am
  • Editorial Team
    Editorial Team added an answer It sounds like you want to use the Silverlight animation… May 16, 2026 at 11:45 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I have an application that will be spawning multiple threads. However, I feel there
I know that concurrently accessing the same object from different threads, without synchronisation, is
Does Java have (or is there a library available) that allows me to have
I created two (or more) threads to insert data in a table in database.
I have two table Part and SubPart. Part table has general fields like id,
Some time ago we needed a solution for Single Sign On authentication between multiple
Ok, So I have these two tables - BioUser- UserId,Weight,DateAdded DimDate-Date // It has
I want to use Lucene.NET for fulltext search shared between two apps: one is
Now I'm in such a situation that there is a group of predefined tasks
I am not sure what the most appropriate way to handle JPA objects that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.