线程安全的单例模式

来源:互联网 发布:手动 备份 hdfs 数据 编辑:程序博客网 时间:2024/06/04 01:34

1. 单例模式

这里有单例模式的wiki定义:

In software engineering, the singleton pattern is a software design pattern that restricts the instantiation of a class to one object. This is useful when exactly one object is needed to coordinate actions across the system. The concept is sometimes generalized to systems that operate more efficiently when only one object exists, or that restrict the instantiation to a certain number of objects. The term comes from the mathematical concept of a singleton.
在软件工程领域,单例模式是一种限制某个类只实例化一个类对象的设计模式。

1). Eager load mode: 在类加载时初始化。

package com.fqyuan.singleton;public class Singleton_Eager {    private Singleton_Eager() {    }    private static Singleton_Eager _instance = new Singleton_Eager();    public static Singleton_Eager getInstance() {        return _instance;    }}

2). Lazy-load: 在第一次调用getInstance()方法时初始化

package com.fqyuan.singleton;public class Singleton_Lazy {    private Singleton_Lazy() {    }    private static Singleton_Lazy _instance = null;    public static Singleton_Lazy getInstance() {        if (_instance == null)            _instance = new Singleton_Lazy();        return _instance;    }}

2. 同步的单例模式

package com.fqyuan.singleton;public class Singleton_Mul {    private Singleton_Mul() {    }    private volatile static Singleton_Mul _instance = null;    // 1. If two concurrent thread come to 'line1' simultaneously and find    // _instance=null, then two instances    // is created.    public static Singleton_Mul getInstanceNotSafe() {        if (_instance == null) // Line flag: line1            _instance = new Singleton_Mul();        return _instance;    }    // 2. synchronized function.    public synchronized static Singleton_Mul getInstanceSyn() {        if (_instance == null)            _instance = new Singleton_Mul();        return _instance;    }    /*     * 3. double-check-lock thread safe. why is double check required? If two     * concurrent thread come to 'line2' simultaneously, find _instance=null,     * one got the lock and initialize, after that, another thread resumed. if     * no inner if statement, two instance may be created. One more thing to     * keep in mind is to make _instance volatile!     */    public static Singleton_Mul getInstanceDCL() {        if (_instance == null) { // line flag: line2            synchronized (Singleton_Mul.class) {                if (_instance == null) // line flag: line3                    _instance = new Singleton_Mul();            }        }        return _instance;    }}

3. 双重检验锁Double checking lock

/*     * 3. double-check-lock thread safe. why is double check required? If two     * concurrent thread come to 'line2' simultaneously, find _instance=null,     * one got the lock and initialize, after that, another thread resumed. if     * no inner if statement, two instance may be created. One more thing to     * keep in mind is to make _instance volatile!     */    public static Singleton_Mul getInstanceDCL() {        if (_instance == null) { // line flag: line2            synchronized (Singleton_Mul.class) {                if (_instance == null) // line flag: line3                    _instance = new Singleton_Mul();            }        }        return _instance;    }

4. 懒加载的单例模式

package com.fqyuan.singleton;/* * Why is Initialization on demand is thread safe here? * https://stackoverflow.com/questions/6109896/singleton-pattern-bill-pughs-solution * 1. The JLS(java language specification) guarantees that a class is only loaded  *    when it is used for the first time(Make the initialization lazy). * 2. And class loading is thread safe, which makes getInstance() thread-safe. */public class Singleton_IniOnDe {    private Singleton_IniOnDe() {    }    private static class LazyHolder {        private static final Singleton_IniOnDe _instance = new Singleton_IniOnDe();    }    public static Singleton_IniOnDe getInstanceIniOnDemand() {        return LazyHolder._instance;    }}
原创粉丝点击