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 -- Concurrent Access Issue
 
Subject: What we can learn from Singleton Pattern -- Concurrent Access Issue
Author: Alex_Raj
In response to: Singleton Pattern
Posted on: 12/05/2013 11:32:16 PM

What's wrong with the example shown above? Yes, it's not thread-safe.

The singleton pattern must be carefully constructed in multi-threaded applications. If two threads are to execute the creation method at the same time when a singleton does not yet exist, they both end up with creating their own instances -- which contradicts the definition or purpose of singleton at the first place. Moreover, if the creation of singleton involves grabbing a mutual exclusive resource (e.g. disk file holder or database connection), the second instance of the singleton may either gets blocked or holds a null reference to the underlying resource.

There is the thread-safe version:

/**
 * Synchronized on method getInstance()
 */
public class Singleton 
{
    /* static field to ensure only one copy */
    private static Singleton instance;

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

    /* public static method to ensure global point of access */
    public static synchronized Singleton getInstance() 
    {
        if(instance==null){
            instance = new Singleton();
        }
        return instance;
    }

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


OR

/**
 * Synchronized on block
 */
public class Singleton 
{
    /* static field to ensure only one copy */
    private static Singleton instance;

    /* 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() 
    {
	synchronized(Singleton.class){
            if(instance==null){
                instance = new Singleton();
            }
        }
        return instance;
    }

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


 

> On 12/05/2013 07:20:23 PM Alex_Raj wrote:

As its name suggests, singleton is a class that is instantiated only once across your application environment. This is typically accomplished by creating a static field in the class representing the class.

Two key points of a single design pattern:
  • only one instance allowed for a class

  • global point of access to that single instance


  • Here is an example (not right though):
    public class Singleton 
    {
        /* static field to ensure only one copy */
        private static Singleton instance;
    
        /* 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() 
        {
            if(instance==null){
                instance = new Singleton();
            }
            return instance;
        }
    
        public void sayHello() {
            System.out.println("Hello");
        }
    }
    






    References:

     


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