Synchronization of Nested Data Structures between Threads in Java

Posted by Dominik on Stack Overflow See other posts from Stack Overflow or by Dominik
Published on 2011-02-09T06:58:19Z Indexed on 2011/02/09 7:25 UTC
Read the original article Hit count: 403

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.

© Stack Overflow or respective owner

Related posts about java

Related posts about multithreading