go to  ForumEasy.com   
JavaPro
Home » Archive » Message


[Email To Friend][View in Live Context][prev topic « prev post | next post » next topic]
  What we can learn from Singleton Pattern -- There is always an alternative way.
 
Subject: What we can learn from Singleton Pattern -- There is always an alternative way.
Author: Alex_Raj
In response to: What we can learn from Singleton Pattern -- Double-checked Locking Issue
Posted on: 12/05/2013 11:49:13 PM

Early Instantiation Version:

If the program will always need an instance, or if the cost of creating the instance is not too large in terms of time/resources, the programmer can switch to eager initialization, which always creates an instance:

/**
 * Early instantiation version - no synchronization is needed
 */
public class Singleton 
{
    /* static field to ensure only one copy */
    private static final Singleton INSTANCE = new Singleton();

    /* private constructor to prevent it from being instantiated from outside */
    private Singleton() {
    }

    /* public static method to ensure global point of access */
    public static Singleton getInstance() {
        return INSTANCE;
    }

    public void sayHello() {
        System.out.println("Hello");
    }
}


This version has a number of advantages:
  • The instance is not constructed until the class is used.
  • There is no need to synchronize the getInstance() method, meaning all threads will see the same instance and no (expensive) locking is required.
  • The final keyword means that the instance cannot be redefined, ensuring that one (and only one) instance ever exists.


    Lazy Inner Static Class Version:

    /**
     * Lazy inner static class version - no synchronization is needed
     */
    public class Singleton 
    {
        /* static class to ensure only one copy & lazy load as is needed*/
        private static class SingletonHolder {
            public static final Singleton INSTANCE = new Singleton();
        }
    
        /* private constructor to prevent it from being instantiated from outside */
        private Singleton() {
        }
    
        /* public static method to ensure global point of access */
        public static Singleton getInstance() {
            return SingletonHolder.INSTANCE;
        }
    
        public void sayHello() {
            System.out.println("Hello");
        }
    }
    


    This version has a number of advantages:
  • The instance is not constructed until getInstance() method is used.
  • There is no need to synchronize the getInstance() method, meaning all threads will see the same instance and no (expensive) locking is required.
  • The final keyword means that the instance cannot be redefined, ensuring that one (and only one) instance ever exists.

     

    > On 12/05/2013 11:41:55 PM Alex_Raj wrote:


    What's wrong with the example shown above then? Let's look at this piece:

            if(instance==null){
    	    synchronized(Singleton.class){
                    if(instance==null){
                        instance = new Singleton();
                    }
                }
            }
    


    It's well known "double-checked locking" trick. But is doesn't work for JDK prior to 1.5.0. The compiler optimization could mess it up by making the assignment of the new Singleton object before all its fields are initialized.

    This is no longer an issue after JDK 1.5.0.





    References:

  •  


     
    Powered by ForumEasy © 2002-2022, All Rights Reserved. | Privacy Policy | Terms of Use
     
    Get your own forum today. It's easy and free.