Java Singleton Class

How to create a class that has only one object?

Questions by sumon_123

Showing Answers 1 - 2 of 2 Answers

There r several way to create Singleton object:
1. By using single static private object & using a static method while returning same object on every call.
2. using a static boolean flag for checking the internal static singleton object is initialized or not and returning same object if it is true.
3. (Advanced) Using ThreadLocal class. This is real singleton object. No other factory classs cant create same object again.

refer my previos rep:

Method 3 is a way to create per thread singleton object. ie. every thread will have its own singleton object for its own disposal.

Method 2,1 will fall into a waiting state for singleton object until freed from other thread, adding to performance issue.

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions