android - 单例模式 发表于:2016-03-27,更新于:2017-08-26,By Sally 大纲 1. 懒汉模式1.1. 一般用的比较多的,双重锁判断的1.2. 静态内部类的写法 懒汉模式一般用的比较多的,双重锁判断的 注意关键字volatile 1234567891011121314public class Singleton { private volatile static Singleton instance; private Singleton() {} public static Singleton getInstance() { if(instance == null) { synchronized(Singleton.class) { if(instance == null) { instance = new Singleton(); } } } return instance; }} 静态内部类的写法1234567891011public class Singleton { private Singleton() {} private statice class SingletonLoader { private static final Singleton INSTANCE = new Singleton(); } public static Singleton getInstance() { return SingletonLoader.INSTANCE; }}