3

I make singleton class and use this class object in different class this code work fine in eclipse but when i make runnable jar than it take empty hashmap list i don't know why my code...

My singleton class

public class PointCalculate {

  public HashMap<String, Float> calPoint;
  private static PointCalculate instance;

  private PointCalculate(){
     calPoint = new HashMap<String, Float>();
  }

  public static PointCalculate getInstance(){
     if(instance==null){
        instance = new PointCalculate();
     }
     return instance;
  }

  public void calculatePoint(String uid ,float point){

     Float ps = instance.calPoint.get(uid);
     if(ps==null) {
        ps = point;
        instance.calPoint.put(uid, ps);
     }
     else {
        ps = point+ps.floatValue();
        instance.calPoint.put(uid, ps);
     }
  }
}

and i am passing value from this class below....

  public class Exp {

     public void setpoint(){
        PointCalculate obj = PointCalculate.getInstance();
        obj.calculatePoint(rowkey, point);//rowkey and point come from file.....
     }
  }

now i am passing hashmap....

  public static void main(String args[]) throws Exception {
     PointCalculate obj = PointCalculate.getInstance();
     SqlInsertPoint.givePoint(obj.calPoint);
  }

but in SqlInsertPoint.givePoint() hashmap list will be empty i don't know why if any body know than help me Thanks in advance

Tanmay Patil
  • 6,882
  • 2
  • 25
  • 45
Rishi Dwivedi
  • 908
  • 5
  • 19
  • Well, my first thought is that your factory method is not thread safe, so you cannot guarantee that your object is a singleton. – Aurand Mar 19 '14 at 05:27
  • @Aurand please give me some hint i am not getting because i have never work in thread and i am not using any thread method – Rishi Dwivedi Mar 19 '14 at 05:34
  • @RishiDwivedi Are you calling the Exp.setPoint before calling the SqlInsertPoint.givePoint? If not then this is your issue. Singleton does not save data automatically on disk. So every time the Singleton object will be disposed (every time your program terminates), you will lose it state and the information it holds in memory. If you need to save your data, then you need to save it to the disk (using DB, serialization, so on..) before the termination of the program. – m1o2 Mar 19 '14 at 06:37
  • private final static PointCalculate instance= new PointCalculate(); <- would be a little bit more thread safe. – MemLeak Mar 19 '14 at 06:55
  • This might be due the issue with program execution flow.Please make sure execution is sequential and only single instance of PointCalculate is created by adding some log. – Prasobh.Kollattu Mar 19 '14 at 08:47

1 Answers1

0

What is wrong with this code? In main you obtain an instance of PointCalculate, do not put any points into it, and pass it over to givePoint method. Since you didn't populate the HashMap, it should be empty.

On a separate note, static Singletons are difficult to get right, and in general should be avoided (couple good reasons). In your concrete case not only PointCalculate class is not thread-safe, but it also exposes calPoint to the whole world. So, anybody can run the following code and essentially override your instance.

PointCalculate.getInstance().calPoint = new HashMap();

Community
  • 1
  • 1
mkalkov
  • 1,364
  • 1
  • 11
  • 12