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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T23:47:47+00:00 2026-05-19T23:47:47+00:00

I have a cache implementation like this: class X { private final Map<String, ConcurrentMap<String,

  • 0

I have a cache implementation like this:

class X
{
  private final Map<String, ConcurrentMap<String, String>> structure = new HashMap...(); 

  public String getValue(String context, String id)
  {
     // just assume for this example that there will be always an innner map
     final ConcurrentMap<String, String> innerStructure = structure.get(context);

     String value = innerStructure.get(id);
     if(value == null)
     {
       synchronized(structure)
       {
          // can I be sure, that this inner map will represent the last updated
          // state from any thread?
          value = innerStructure.get(id);
          if(value == null)
          {
            value = getValueFromSomeSlowSource(id);
            innerStructure.put(id, value); 
          }
       }
     }       
     return value;
  }
}

Is this implementation thread-safe? Can I be sure to get the last updated state from any thread inside the synchronized block? Would this behaviour change if I use a java.util.concurrent.ReentrantLock instead of a synchronized block, like this:

...
if(lock.tryLock(3, SECONDS))
{
  try
  {
    value = innerStructure.get(id);
    if(value == null)
    {
      value = getValueFromSomeSlowSource(id);
      innerStructure.put(id, value); 
    }
  }
  finally
  {
    lock.unlock();
  }
}
...

I know that final instance members are synchronized between threads, but is this also true for the objects held by these members?

Maybe this is a dumb question, but I don’t know how to test it to be sure, that it works on every OS and every architecture.

  • 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-19T23:47:47+00:00Added an answer on May 19, 2026 at 11:47 pm

    For starters, this isn’t a dumb question. Synchronization is really hard to get right, and I don’t profess to be an expert in it.

    In your program, at the indicated context, yes, you can assume that the String you’re getting is the most updated version. However, your code is still not safe because you are reading a value from the Map outside of the synchronized block. If this read occurs at the same time that the Map is having a value inserted into it, you’re not guaranteed to get back a sensible value. I know that on at least some implementations, this can cause an infinite loop due to some weirdness in the implementation.

    The short version is that you should not have a structure that is read from or written to by multiple threads unless you guard it with a synchronization primitive like synchronized or a lock, or unless that structure is specifically designed to be lock-free like the ConcurrentHashMap.

    You could indeed use the ReentrantLock in this case to guard access to the structure and to do a timed wait, but if you do you’d have to guarantee that any reads of the structure were also guarded by the same lock. Otherwise you risk multiple threads seeing inconsistent or corrupted data.

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

Sidebar

Related Questions

I have legacy code that has cache implementation that looks like this: long lastUpadate;
I am currently implementing cache. I have completed basic implementation, like below. What I
we have a cache which I would like to put some transaction scopes around
i would like to have a simple cache that supports transactions. With simple I
This is about asp.net mvc3 web application. We have used Object cache to store
I have a class used to cache access to a database resource. It looks
I'm very new to multi-threading and for some reason this class is giving me
I have a list of objects like this: [ Rectangle(20, 30, 100, 200), //
I would like to have several Cache objects in my application and I'd like
I have cache static resources of my website using manifest file But when I

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.