Singleton:
- That is, throughout the life cycle for the production of the object is always a, never change.
- To ensure that only one instance of a class, and provide a global access point to access it.
effect:
- In the case of thread-safe requirements to ensure the uniqueness of thread safety class instance.
- When not required the presence of multiple instances, to ensure the unity of class instance. Do not waste memory.
Features:
- The method of obtaining the public instance,
- Private constructor,
- Private member variables.
A, starving the formula
* @Description single embodiment starving
* starving single embodiment singleton key is obtained directly as a class variable and initialized, i.e. a class all variables are initialized
* as singleton class variable initialization process are collected into <clinit> () method, which can ensure the synchronization hundred percent,
* but because it is not lazy loading, Singleton, after being loaded may be a long time is not used, i.e., examples of the open space will exist long time
* Although the only instance of multiple threads can be achieved, but can not lazily;
package com.liruilong.singleton; /** * @Author: Liruilong * @Date: 2019/7/20 17:55 */ // Final allowed to be inherited public Final class the Singleton { // instance variables Private byte [] = Bate new new byte [1024 ]; // private constructor, which does not allow the external new new Private the Singleton () {} Private static Final the Singleton = singleton1 new new the Singleton (); public static the Singleton getInstance1 () { return singleton1; }
Second, lazy style
* @Description lazy man singleton
* lazy loading can be guaranteed, but not thread-safe
* When there are two threads access, does not guarantee the uniqueness of a single case
package com.liruilong.singleton; /** * @Author: Liruilong * @Date: 2019/7/20 17:55 */ // Final allowed to be inherited public Final class the Singleton { // instance variables Private byte [] = Bate new new byte [1024 ]; // private constructor, which does not allow the external new new Private the Singleton () {} Private static the Singleton Singleton = null ;
public static the Singleton the getInstance () { IF (Singleton == null ) { singleton = new Singleton(); } return singleton; }
Third, add synchronization method lazy formula
* + @Description lazy formula synchronization method Singleton
* that is able to guarantee lazy loading, and can guarantee the uniqueness of singleton instances, but results in the exclusive keyword synchronizeed
* getInstance0 () in the same method can only time is a thread access. Poor performance.
package com.liruilong.singleton; /** * @Author: Liruilong * @Date: 2019/7/20 17:55 */ // Final allowed to be inherited public Final class the Singleton { // instance variables Private byte [] = Bate new new byte [1024 ]; // private constructor, which does not allow the external new new Private the Singleton () {} Private static the Singleton Singleton = null ;
public static the synchronized the Singleton getInstance0 () { IF (Singleton == null ) { singleton = new Singleton(); } return singleton; }
Fourth, the double lock singleton efficacy
* + Volatile @Description double check single lock Example (Double-the Check)
* for lazy - improved synchronization method, when there are found two threads singleton is null, only one thread can access the synchronization block in.
* Lazy loading is met, and ensures the uniqueness of the thread
shortcomings * without volition, and sometimes may report NPE, (JVM run command reordering)
* Examples of possible variables to instantiate an object unfinished other threads to acquire to singleton variable.
* Initialization unfinished instance calls its method throws a null pointer exception.
package com.liruilong.singleton; /** * @Author: Liruilong * @Date: 2019/7/20 17:55 */ // Final allowed to be inherited public Final class the Singleton { // instance variables Private byte [] = Bate new new byte [1024 ]; // private constructor, which does not allow the external new new Private the Singleton () {} private static volatile Singleton singleton2 = null;
public static Singleton getInstance4() { if (singleton2 == null){ synchronized (Singleton.class){ if (singleton2 ==null){ singleton2 = new Singleton(); } } } return singleton2; }
Fifth, a single static inner classes Example
* @Description static inner class Singleton
* Singleton does not create instances Singleton class initialization, defines a singleton instance of the static inner classes.
* When the active to create a static class is created inside Singleton static variable, one of the best singleton
package com.liruilong.singleton; /** * @Author: Liruilong * @Date: 2019/7/20 17:55 */ // final 不允许被继承 public final class Singleton { // 实例变量 private byte[] bate = new byte[1024]; // 私有的构造函数,即不允许外部 new private Singleton(){ } private static class Singtetons{
private static Singleton SINGLETON = new Singleton(); /* static { final Singleton SINGLETON = new Singleton(); }*/ } public static Singleton getInstance2(){ return Singtetons.SINGLETON; }
六,枚举类单例
* @Description 基于枚举类线程安全
* 枚举类型不允许被继承,同样线程安全的,且只能被实例化一次。
package com.liruilong.singleton; /** * @Author: Liruilong * @Date: 2019/7/20 17:55 */ // final 不允许被继承 public final class Singleton { // 实例变量 private byte[] bate = new byte[1024]; // 私有的构造函数,即不允许外部 new private Singleton(){ } private enum Singtetonss { SINGTETONSS; //实例必须第一行,默认 public final static修饰 private Singleton singleton; Singtetonss() { //构造器。默认私有 this.singleton = new Singleton(); } public static Singleton getInstance() { return SINGTETONSS.singleton; } } public static Singleton getInstance3(){ return Singtetonss.getInstance(); }
原本是我笔记里的,摘了出来,面试的时候看,
更多见 ----》《Java并发编程详解》读书笔记