Java implementation Singleton pattern and the difference

Villains singleton pattern:
/ *

  • Villains singleton pattern, with space for thinking time to initialize the object, in the case of multi-threaded and thread-safe so there is no problem.

    • @author Administrator
    • */
      public class WickedManSingleTon {

    private static WickedManSingleTon instance=new WickedManSingleTon();

    private WickedManSingleTon() {

    }

    public static WickedManSingleTon getIntance() {
    return instance;
    }

}

Lazy mode:

/**

  • Single idler embodiment mode, a time for space concept, examples of singletons, in the case of multiple threads, thread safety problem exists.

    • @author Administrator
    • */
      public class LasyManSingleTon {

    private static LasyManSingleTon instance=null;

    private LasyManSingleTon() {

    }

    public static LasyManSingleTon getInstance() {
    if(instance==null) {
    instance=new LasyManSingleTon();
    }
    return instance;
    }

}

/**

  • Double retrieval achieve Singleton
  • Advantage @author Administrator DCL mode is that only when the object needs to be used to create, for the first time to determine INSTANCE ==
  • In order to avoid unnecessary null lock, only when the first load lock for instantiating instances. This will not only save memory space, and can ensure thread safety. However, due to order execution functions jvm, DCL thread unsafe situation will arise. Specific analysis is as follows:
  • INSTANCE = new DCLInstance();
  • This step, in fact, inside jvm execution of three steps:   
  • 1. In open heap memory space.  
  • 2. In each of the examples of the heap memory parameter SingleTon inside.  
  • 3. The object pointing to the heap memory space.
  • Because of order execution JVM functions, it may not execute at 2 is performed on the first 3, then if this time is switched to the thread B, due to the execution of 3, INSTANCE
  • Has a non-empty, will be used directly out, this is the case, there will be an exception. This is the famous DCL failures.
    * /
    Public class DCLInstance {
    // handwritten dual retrieve
    private static DCLInstance instance = null; // optimization using volatile

    private DCLInstance() {

    }

    public static DCLInstance getInstance() {

    if (instance == null) {
        // 同步操作
        synchronized (DCLInstance.class) {
            if (instance == null) {
                // 多线程环境下可能会出现问题的地方
                instance = new DCLInstance();
            }
        }
    }
    return instance;

    }

}

/**

  • @author Administrator
  • As internal class that implements the singleton pattern:
  • Advantage is static inner classes: the class does not need to load the external load immediately inner class, the class is not loaded inside the INSTANCE not initialized, it does not account for memory.
  • That is, when SingleTon is first loaded, do not need to load SingleTonHoler, only when the getInstance () method is invoked for the first time,
  • Will go initialization INSTANCE, the first call getInstance () method causes the virtual machine to load SingleTonHoler class,
  • This approach not only thread-safe, but also to ensure the uniqueness of a single case, but also delay the instantiation of the singleton.
  • */
    public class InerClassMakeIntance {

    private static InerClassMakeIntance instance =null;

    private InerClassMakeIntance() {

    }

    public static InerClassMakeIntance getInstance() {
    return InerInstance.t1;
    }

    private static class InerInstance {
    private static InerClassMakeIntance t1 = new InerClassMakeIntance();
    }
    }

Guess you like

Origin blog.51cto.com/13217372/2437525