0

I have a code sample with static methods and singleton class

//code with static methods
public class DataManager{
  public static Object getDataObject(){
     HashMap map = new HashMap();
     //Some processing in collections
     //retrieval of data
  }
  public static void writeData(Object o){
     HashMap map = new HashMap();
     //Some processing in collections
     //writing data
  }
}
//code with singleton
public class DataManager{
   private static DataManager instance;
   private DataManager(){
   }
   public static DataManager getInstance(){
     if(instance == null){
        synchronized(DataManager.class){
          if(instance == null){
              instance = new DataManager();
          }
       }
    }
    return instance;
  }
  public Object getDataObject(){
     HashMap map = new HashMap();
     //Some processing in collections
     //retrieval of data
  }
  public writeData(Object o){
     HashMap map = new HashMap();
     //Some processing in collections
     //writing data
  }
}

Which is the best way to use. What happens when 2 threads calls one of my methods? Is there any way my data could get corrupt while processing on collections? static method has common memory allocated for it, so when 2 threads calls a static method will it affect each other? in singleton only one instance is created, if 2 threads calls a method with a single instance will it affect each other? Please help to understand this. Thank you...

3 Answers3

0

Calling a static method from different Threads doesn't corrupt anything... unless method is using/altering shared instance or static variables, but in that case it is true for all methods, not just methods with static nature.
It's not the first time somebody got confused in this... Please google it or search in Stackoverflow for similar questions. This question is answered multiple times.

Manoj
  • 76
  • 6
0

Whether you use static methods or a singleton does not influence thread safety. Your code might be thread safe, or it might not, depending on what kind of processing you do. (As shown, your code is thread safe because the maps are created local to the getDataObject and writeData methods, and thus are not shared between different threads.)

In general, code is thread safe if different threads do not access the same data at the same time. Also, if an object is read-only, multiple threads may read it at the same time without breaking thread safety. As soon as one thread modifies the shared data you have to synchronize the access to it.

Hoopje
  • 12,677
  • 8
  • 34
  • 50
  • Even if i use some other collections and objects of other classes to process data, there won't be any corruption in data? Explain it in memory wise, where it gets stored for each thread and how the data won't be corrupted. – Rishikesan Varudharajan Sep 29 '15 at 09:05
  • I wrote: "Your code might be thread safe, or it might not, *depending on what kind of processing you do.*" So, sure, if you access other data, then it might not be thread-safe. But the code in the question only contains comments. – Hoopje Sep 29 '15 at 09:08
  • getDataObject(){ HashMap map = new HashMap(); Query query = QueryBuilder.select(map); ResultSet result = query.execute(); return result.parseResult; } some code like this will be there instead of comments – Rishikesan Varudharajan Sep 29 '15 at 09:09
  • 1
    Your database will in general be thread-safe. So it depends what you're doing, in your real code, with that hash map. If you only use it inside your method it is not a shared object and does not break thread-safety. – Hoopje Sep 29 '15 at 19:07
-1

From my understanding on multithreading, it is the shared object which will be accessed by multiple threads.

So in both of your program, the map object is the shared instance which will be accessed by multiple threads. So you should synchronize map object inorder to be thread safe.

Singleton or Static has no way related to this

Lathy
  • 879
  • 1
  • 8
  • 25
  • 2
    I beg to differ with your answer. Map object is method local and will be on thread's own stack. None of the class has a shared state. – Aceghn Feb 23 '16 at 16:39