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 7040619
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:56:23+00:00 2026-05-28T01:56:23+00:00

I wanted to discuss a specific use I have of a concurrent map to

  • 0

I wanted to discuss a specific use I have of a concurrent map to sense check my logic…

If I used ConcurrentHashMap, I can do the familar

private final ConcurrentHashMap<K, V> map = new ConcurrentHashMap<K, V>();

public V getExampleOne(K key) {
    map.putIfAbsent(key, new Object());
    return map.get(key);
}

but I realise that a race condition exists whereby if I remove the item from the map between the putIfAbsent and the get, the method above would return something that no longer exists in the collection. This may or may not be fine, but lets assume that for my use case, it’s not ok.

What I’d really like is to have the whole thing atomic. So,

public V getExampleTwo(K key) {
    return map.putIfAbsent(key, new Object());
}

but as this expands out to

if (!map.containsKey(key))
   return map.put(key, value);     [1]
return map.get(key);

which for line [1] will return null for first usage (ie, map.put will return the previous value, which for first time use is null).

I can’t have it return null in this instance

Which leaves me with something like;

public V getExampleThree(K key) {
    Object object = new Object();
    V value = locks.putIfAbsent(key, object);
    if (value == null)
        return object;
    return value;
}

So, finally, my question; how do the examples above differ in semantics?. Does getExampleThree ensure atomicity like getExampleTwo but avoid the null return correctly? Are there other problems with getExampleThree?

I was hoping for a bit of discussion around the choices. I realise I could use a non ConcurrentHashMap and synchronize around clients calling my get method and a method to remove from the map but that seems to defeat the purpose (non blocking nature) of the ConcurrentHashMap. Is that my only choice to keep the data accurate?

I guess that’s a bit part of why you’d choose ConcurrentHashMap; that its visible/up-to-date/acurrate at the point you interact with it, but there may be an impact further down the line if old data is going to be a problem…

  • 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-28T01:56:24+00:00Added an answer on May 28, 2026 at 1:56 am

    It sounds like you are trying to create a global lock object for a key.

    Instead of deleting an entry with the possibility of have it re-created almost immediately, I would only delete the entry when you pretty sure its not needed ever again.

    Otherwise, if you are using this for a lock, you can have two thread locking on different objects for the same key.


    If its not ok, you can busy loop it.

    public V getExampleOne(K key) {
        for(Object o = null, ret = null; (ret = map.get(key)) == null; )
            map.putIfAbsent(key, o == null ? o = new Object() : o);
        return ret;
    }
    

    it can still be removed or replaced as soon as the loop exists so its effectively much the same as.

    public V getExampleThree(K key) {
        Object o = new Object();
        map.putIfAbsent(key, o);
        Object ret = map.get(key);
        return ret == null ? o : ret;
    }
    

    So, finally, my question; how do the examples above differ in semantics?.

    The difference is only the obvious.

    Does getExampleThree ensure atomicity like getExampleTwo but avoid the null return correctly?

    Yes.

    Are there other problems with getExampleThree?

    Only if you believe the very next call might not give you a different value (if you believe it can be removed in another thread)

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

Sidebar

Related Questions

I wanted to open up the topic to discuss ways to encourage or incentivize
I wanted to use 6 different textures on a cube, one per side, but
I wanted to create an IDN-aware formencode validator to use in one of my
I wanted to make a special version of shared_ptr that would perform specific operations
Wanted to use the same URL for a GET/PUT/DELETE/POST for a REST based API,
Just wanted to know if it is better to have Accept-Encoding, gzip in request
I wanted to use RavenDB as back-end for my website . but right now
Wanted to add an alias for one of the charsets that PayPal may use
I have a question to discuss - page performance and speed when adding additional
wanted to see if somebody can provide some suggestions/pointers on how to address this

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.