Will accesing static object/resources through static methods have performance issue?

I have a class which reads an xml file and populates them in a private static data-structure(say, HashMap). This initial population happens in a static block. Then I have method to get value of a given key, which intern refers that static HashMap. Cosider the case, when multiple threads tries to get value for a given key, will there be any performance hit; like, when one thread is reading that static object other threads has to wait.

  public class Parser
  {
       private static HashMap resource = new HashMap();

       static
       {
         parseResource();
       }

       private Parser()
       {
       }

       private static parseResource()
       {
           //parses the resource and populates the resource object
       }

       public static Object getValue( String key)
       {
           //may be some check will be done here, but not any     
           //update/modification actions
          return resource.get(key);
       }    
   }
Jon Skeet
people
quotationmark

Firstly, it's worth being aware that this really has very little to do with static. There's no such thing as a "static object" - there are just objects, and there are fields and methods which may or may not be static. For example, there could be an instance field and a static field which both refer to the same object.

In terms of thread safety, you need to consider the safety of the operations you're interested in on a single object - it doesn't matter how the multiple threads have "reached" that object.

like, when one thread is reading that static object other threads has to wait.

No, it doesn't.

If you are just reading from the HashMap after constructing it in a way that prevented it from being visible to other threads until it had been finished, that's fine. (Having reread your comment, it looks like that's the case in getValue.)

If you need to perform any mutations on the map while other threads are reading from it, consider using ConcurrentHashMap or use synchronization.

From the docs for HashMap:

Note that this implementation is not synchronized. If multiple threads access a hash map concurrently, and at least one of the threads modifies the map structurally, it must be synchronized externally.

people

See more on this question at Stackoverflow