//Single类一进内存,就已经创建好了对象。先吃
class Single//开发用这个,它安全简单
{
private static Single s = new Single();
private Single(){}
public static Single getInstance()
{
return s;
}
}
//对象是方法被调用时,才初始化,也叫做对象的延时加载。成为:懒汉式。饿了在吃
//Single类进内存,对象还没有存在,只有调用了getInstance方法时,才建立对象。
class Single
{
private static Single s = null;//private final static Single s = null;
private Single(){}
public static Single getInstance()
{
if(s==null) //如果多个人同时来了就有问题了,所以同步加上了,但影响性能
{
synchronized(Single.class)
{
if(s==null)
s = new Single();
}
}
return s;
}
}