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

  • SEARCH
  • Home
  • 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 6648727
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T00:41:21+00:00 2026-05-26T00:41:21+00:00

I have a HashMap and want to synchronize each row/entry separately in order to

  • 0

I have a HashMap and want to synchronize each row/entry separately in order to maximize concurrency, so in this way many threads can access the HashMap at the same time but no two threads or more can access the same row/entry at the same time.

I did the following in my code but I’m not sure if it’s correct or not:

/* Lock/synchronize the data to this key, (skey is a key of type String) */
synchronized (aHashMap.get(skey)) {

    /* write the data (data is Integer) */
    aHashMap.put(skey, data);

}
  • 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-26T00:41:22+00:00Added an answer on May 26, 2026 at 12:41 am

    The appropriate solution depends very much on your particular problem. If all your threads can update any of the entries in the Map, then the first thing to try is ConcurrentHashMap:

    In this case, the operation you described would be replaced with:

    data = ... compute ...
    aHashMap.replace(skey, data);
    

    Using ConcurrentHashMap solves the data race but one problem remains. If another thread would update the same key at the same time, one of the computations would be lost. If you are ok with this, great. Otherwise, you can:

    do {
      oldData = aHashMap.get(skey);
      data = ... compute (maybe based on oldData) ... 
      boolean success = aHashMap.replace(skey, oldData, data);
    } while(!success);
    

    In this case, replace will only succeed if the data hasn’t changed (and the replace would be atomic). If if fails, you can put everything in a do while loop to try again, maybe based on the updated value.

    Also, be careful not to have any side effects between the map get and replace. that computation should only create a brand new “data” object. If you update the “oldData” object or some other shared data you will get unexpected results.

    If you do have side effects, one approach is to have make a key-level lock like this:

    synchronized(skey) {
      data = ... compute ... 
      aHashMap.replace(skey, data);
    }
    

    Even in this case, ConcurrentHashMap is still needed. Also, this will not stop some other code from updating that key in the map. All code that updates the key would need to lock on it.

    Also, this will not be thread-safe if you update oldData in “… compute …” and the values are not unique within the map. If you do want to update oldData there, cover it with another synchronized.

    If this does the trick and your content with the performance, look no further.

    If the threads only update values, do not change the keys, then you might try converting your pairs to objects and use something different than a Map. For example, you could split the set of objects in several sets and then feed them to your threads. Or maybe use ParallelArray. But I might be digressing here… 🙂

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

Sidebar

Related Questions

If I have a Map like this: HashMap<Integer, ComparableObject> map; and I want to
I have a HashMap (although I guess this question applies to other collections) of
i have a hashmap where every key has many values(stored in a arraylist). How
If I have a HashMap that looks like this: HashMap<String, MyObject> where the String
I have a simple class that fills in a simple hashmap I want to
I have a HashMap<String,String> and there is a static method which returns this map
i have a big problem... i have this map HashMap<Long, String> examValues; It gets
I have a hashmap that contains 10 children hashmap, each of these child hashmap
I have this : HashMap<String, String[]> strs = new HashMap<String, String[]>(); String [] morning_food
I have data in a hashmap, and I want to create a histogram over

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.