/** * Created by jinyangyang on 2019/5/8. */ public class Singleton { private static Singleton singleton = null; //饿汉式单例模式 //Singleton singleton = new Singleton(); private Singleton(){ } //饿汉式单例模式 /*public static synchronized Singleton getInstance(){ return singleton; }*/ //懒汉式单例模式 /*public static synchronized Singleton getInstance(){ if(singleton == null){ singleton = new Singleton(); } return singleton; }*/ //线程安全单例模式 /*public static Singleton getInstance(){ synchronized (Singleton.class){ if(singleton == null){ singleton = new Singleton(); } } return singleton; }*/ //双重索检查,提升性能 public static Singleton getInstance(){ if(singleton == null){ synchronized (Singleton.class){ if(singleton == null){ singleton = new Singleton(); } } } return singleton; }