0

Assuming that we have a thread safe singleton class in Java, which would be the best or better way to use it. For instance, consider that we have a Singleton class as:

public class MySingleTon {
   // my thread-safe code goes here
}

And we have a service class that uses the above singleton in the two possible ways as follows :

1:

public class MyService {
   MySingleton singleton;

   public void myMethod1() {
      singleton  = MySingleton.getInstance();
      // code ...
   }

   public void myMethod2() {
      singleton = MySingleton.getInstance();
      // code...
   }
   public void myMethod3() {
      singleton = MySingleton.getInstance();
      // code...
   }
}

or

2:

public class MyService {

   public void myMethod1() {
      MySingleton singleton = MySingleton.getInstance();
      // code ...
   }

   public void myMethod2() {
      MySingleton singleton = MySingleton.getInstance();
      // code...
   }
   public void myMethod3() {
      MySingleton singleton = MySingleton.getInstance();
      // code...
   }
}

In case one the singleton is referenced via an instance variable and in case 2, the singleton is referenced as a method local variable. Considering that both the above snippets are used in multi-threaded environment, which is a better option and why?

bplpu
  • 464
  • 4
  • 21
user3244615
  • 330
  • 1
  • 15
  • This question is very broad since we don't know any details about your singleton and how exactly are you going to use it. For instance: Is singleton instance mutable, are `myMethod`s going to change state of singleton, can they be synchronized? – Pshemo Jan 23 '15 at 18:19
  • When I say that it's thread-safe (immutable or mutable with synchronized access to mutable state), doesn't it covers all the cases? Not that I am undermining your question here, but just wondering if I missed anything here. – user3244615 Jan 23 '15 at 18:41
  • I mean even if some class is considered thread-safe we can still use it in non-thread safe way, for instance `method1` can set its own state of singleton and do some other things based on assumption that this state stays. But in the same time `method2` can try to change this state to something else. Would it be problem in your case? Would it be possible? – Pshemo Jan 23 '15 at 19:10
  • My singleton has a reference to another stateless class which opens a transaction (TitanTransaction) to a titan graph database. – user3244615 Jan 23 '15 at 19:26
  • @JohnVint said, "...no difference in thread-safety". The most dubious case is your example 2 because you allow different threads to update the `singleton` variable. But even that case is completely safe because the threads always update it with the same object reference. Java guarantees that when a thread reads a reference variable, it will always see a value that was stored at some previous time by the program, and since there's only one value that is ever stored... You do the math. – Solomon Slow Jan 23 '15 at 19:59

2 Answers2

2

I would define it a third way

public class MyService {
   final static MySingleton singleton = MySingleton.getInstance();

   public void myMethod1() {
      // code ...
   }

   public void myMethod2() {
      // code...
   }
   public void myMethod3() {
      // code...
   }
}

In all cases there is no difference in thread-safety

John Vint
  • 39,695
  • 7
  • 78
  • 108
0

2 would be better in my opinion. It gives MySingleton freedom to kill the singleton instance and recreate if needed (i.e., self-detecting deadlock or resource contention mitigation).

Danny Daglas
  • 1,501
  • 1
  • 9
  • 9