Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * An interface defining objects that can create Singleton
- * instances.
- */
- public interface SingletonFactoryFunctor {
- /**
- * @return An instance of the Singleton.
- */
- public Singleton makeInstance();
- }
- public static synchronized Singleton getInstance()
- {
- // since whole method is synchronized only 1 thread can
- // enter the following block ensuring only one instance
- // is ever created. however we pay a synchronization
- // penalty every time this method is called.
- if(mInstance==null) {
- mInstance=new Singleton();
- }
- return mInstance;
- }
- // no synchronization penalty, but penalty for eager init
- private static Singleton mInstance = new Singleton();
- public static Singleton getInstance()
- {
- return mInstance;
- }
- private volatile static Singleton mInstance;
- private Singleton(){}
- public static Singleton getInstance()
- {
- if(mInstance==null) {
- synchronized (Singleton.class) {
- // only pay synchronization penalty once
- if(mInstance==null){
- mInstance=new Singleton();
- }
- }
- }
- return mInstance;
- }
- public class Singleton {
- private static Singleton mInstance;
- private Singleton(){}
- public static Singleton getInstance()
- {
- if(mInstance==null){
- mInstance=new Singleton();
- }
- return mInstance;
- }
- }
Add Comment
Please, Sign In to add comment