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?

5
  • 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 myMethods going to change state of singleton, can they be synchronized? Commented Jan 23, 2015 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. Commented Jan 23, 2015 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? Commented Jan 23, 2015 at 19:10
  • My singleton has a reference to another stateless class which opens a transaction (TitanTransaction) to a titan graph database. Commented Jan 23, 2015 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. Commented Jan 23, 2015 at 19:59

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

2 Comments

could even be a constant IMHO
Yup, could be static.
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).

3 Comments

Are there any chances of deadlock in the first case??
If there are no bugs in the thread-safe code, then no. But, that's a big if.
I totally agree. Really a big if. :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.