线程安全的单例模式

来源:互联网 发布:伊朗 沙特 知乎 编辑:程序博客网 时间:2024/06/01 08:40
public class SingleInstance {    //成员私有化    private static SingleInstance instance;    //构造方法私有化使得只能通过getInstance()获取    private SingleInstance() {    }    public static SingleInstance getInstance() {        if (instance == null) {            synchronized (SingleInstance.class) {                if (instance == null) {                    instance = new SingleInstance();                }            }        }        return instance;    }}
1 0
原创粉丝点击