Java单例设计模式

来源:互联网 发布:excel 筛选重复的数据 编辑:程序博客网 时间:2024/05/21 09:17

Java单例设计模式

单例设计模式 懒汉式/饿汉式

class Single//懒汉式的(多线程)安全定义方法(面试常考){    private static Single s=null;    private Single(){}    public static Single getInstance()    {        if(a==null)        {            synchronized(Single.class)            {                if(s==null)                    s=new Single();            }        }        return s;    }}//懒汉式用于延迟加载
class Single//饿汉式(开发中常用){    private static final Single s=new Single();    private static Single getInstance()    {        return s;    }}
0 0