单例模式 发表于 2017-09-11 [TOC] 单例模式的写法懒汉式不加同步1234567891011121314public class SingletonOne { private static SingletonOne sInstance; private SingletonOne() { } public static SingletonOne getInstance() { if (sInstance == null) { sInstance = new SingletonOne(); } return sInstance; }} 同步1234567891011121314public class SingletonTwo { private static SingletonTwo sInstance; private SingletonTwo() { } public synchronized static SingletonTwo getInstance() { if (sInstance == null) { sInstance = new SingletonTwo(); } return sInstance; }} 双重检查锁123456789101112131415161718public class SingletonThree { private static SingletonThree sInstance; private SingletonThree() { } public static SingletonThree getInstance() { if (sInstance == null) { synchronized (SingletonThree.class) { if (sInstance == null) { sInstance = new SingletonThree(); } } } return sInstance; }} volatile(推荐)123456789101112131415161718public class SingletonFour { private static volatile SingletonFour sInstance; private SingletonFour() { } public static SingletonFour getInstance() { if (sInstance == null) { synchronized (SingletonFour.class) { if (sInstance == null) { sInstance = new SingletonFour(); } } } return sInstance; }} 饿汉式1234567891011public class SingletonFive { private static SingletonFive sInstance = new SingletonFive(); private SingletonFive() { } public static SingletonFive getInstance() { return sInstance; }} 静态内部类(推荐)1234567891011121314public class SingletonSix { private SingletonSix() { } public static SingletonSix getInstance() { return SingletonHolder.INSTANCE; } private static class SingletonHolder { public static final SingletonSix INSTANCE = new SingletonSix(); }} 枚举123public enum SingletonSeven { INSTANCE;} UniversalImageLoader分析 如果您觉得这篇文章不错,可以打赏支持下哦,谢谢 打赏 微信支付 支付宝