Singleton mode is a kind of common software design pattern. In its core structure, there is only one special class that is called a singleton. The singleton mode ensures that the class with the pattern can have only one instance of the class that is used in the system. That is, a class has only one instance of an object.
To implement a singleton in Java, you must understand the Java memory mechanism, where instance objects exist in heap memory, and to implement a singleton, you must meet two conditions:
1. Restrict class instantiation objects. That is, only one object can be produced.
2. Ensure external access to this object, otherwise object creation will be meaningless
To meet the above two conditions, you can privatize the construction method and then provide a static method in the class to get the class instance. The code is as follows
1 Public classSingleTon {2 3 Private Static FinalSingleTon single =NewSingleTon (); 4 5 PrivateSingleTon () {6 7 }8 9 /**Ten * Gets a singleton object. One * @returnreturns the Singleton object. A */ - Public StaticSingleTon getinstance () { - returnSingle ; the } - -}
When the JVM loads SingleTon, the static member is initialized by default, and the object created by New SingleTon () is assigned to single, and the class is loaded only once, even if the getinstance method is called multiple times, the returned object will not change. The single field in the initialization process, the object is created, so the code of the above case is called a hungry man. From the object's life cycle, once the class is loaded, the object is created immediately in the heap, wasting memory space, and therefore there is another kind of singleton design pattern called the lazy type. The code is as follows:
1 Public classSingletonlazy {2 3 Private StaticSingletonlazy single =NULL;4 5 PrivateSingletonlazy () {6 7 }8 9 /**Ten * Gets a singleton object. One * @returnreturns the Singleton object. A */ - Public StaticSingletonlazy getinstance () { - the if(single =NULL) { -Single =NewSingletonlazy (); - } - + returnSingle ; - + } A at}
Singletonlazy The object is not created immediately after it is loaded into the method area, it is not created until the GetInstance method is called. This way can save memory space, but there is also a thread security problem, when thread a execution to the judgment object is null, when thread B gets execution, thread B determines that the object is null, then thread a regain execution, create the object, thread B resumes, continue to create the object. Modify the code as follows, using a synchronous lock to resolve thread safety issues.
1 Public classSingletonlazy {2 3 Private StaticSingletonlazy single =NULL;4 5 Private Final StaticLock lock =NewReentrantlock ();6 7 PrivateSingletonlazy () {8 9 }Ten One /** A * Gets a singleton object. - * @returnreturns the Singleton object. - */ the Public StaticSingletonlazy getinstance () { - - if(Single! =NULL) { - returnSingle ; + } - + Lock.lock (); A if(single =NULL) { atSingle =NewSingletonlazy (); - } - Lock.unlock (); - - returnSingle ; - in } -}
Summary: A hungry man-style and lazy-type each have advantages and disadvantages, but relatively speaking, occupy memory space than the CPU to determine the cost of lock, so the A hungry man-style more use some.
A single-instance design pattern for Java design patterns