Rohit Kejriwal wrote:Hi,
Recently I was asked to make all the use of singleton in our existing java application thread safe. I know Singleton is not thread safe and can lead to the creation of multiple instance if two or more thread accesses the getInstance() block at the same time.
So it doesn't have a GUI? If there truly is only the main thread in the application, then there is no need to add thread safety. It is just really not very common to have only the one thread.Rohit Kejriwal wrote: But I had some doubt for which I will need your opinion.
1. If my application is not using thread at all in the application, is it worth make the singleton usage thread safe?
A singleton with no variables is a singleton with no state. A class with no state should not require an instance, so it should not be a singleton. It should use only static methods, which would make it a Utility class. You should drop the requirement for an instance (or many instances...). Or, you should make it not a singleton and allow as many instances of the class as client code wants, since there is no side effect to doing so. The second option lends itself better to certain designs and testing. But both seem to be suitable choices, and I would lean towards the static Utility class. Either way, no Singleton.Rohit Kejriwal wrote:2. There are singleton classes that has no variables but just few public method, is it worth making these classes thread safe.
Rohit Kejriwal wrote: 3. Is there anything that I need to know before making singleton thread safe?
Thanks in advance for your help.
Steve
Rohit Kejriwal wrote:Recently I was asked to make all the use of singleton in our existing java application thread safe.
If what you said is guaranteed...ALWAYS - No.1. If my application is not using thread at all in the application, is it worth make the singleton usage thread safe?
No.2. There are singleton classes that has no variables but just few public method, is it worth making these classes thread safe.
I think the guys have covered that one for you: make it immutable. And the easiest way to do that is by direct assignment of the instance.3. Is there anything that I need to know before making singleton thread safe?
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Steve Luke wrote:A class with no state should not require an instance, so it should not be a singleton. It should use only static methods, which would make it a Utility class. You should drop the requirement for an instance (or many instances...).
Winston Gutkowski wrote:
If what you said is guaranteed...ALWAYS - No.Rohit Kejriwal wrote:If my application is not using thread at all in the application, is it worth make the singleton usage thread safe?
Rohit Kejriwal wrote:Recently PMD was run against the application and it came up with the Singleton not thread safe violation. And I was asked to work on this. I am currenntly trying to figure out ways to handle these.
Junilu Lacar wrote:I suggest you read through the Wikipedia article on singleton pattern and implement one of the recommended fixes.
Junilu Lacar wrote:
Rohit Kejriwal wrote:Recently PMD was run against the application and it came up with the Singleton not thread safe violation. And I was asked to work on this. I am currenntly trying to figure out ways to handle these.
PMD probably detected one of the broken methods for lazy initialization of a singleton. I suggest you read through the Wikipedia article on singleton pattern and implement one of the recommended fixes.
Steve
Rohit Kejriwal wrote:But I am not able to figure out what will be the best option- making methods Static or allowing multiple instances.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Rohit Kejriwal wrote: making methods Static or allowing multiple instances. How will these effect the performance of the system. This application is a huge e-commerce application with more then 40,000 transaction daily. So application performance will be one of the main criteria to decide on the actual implementation.
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime. |