1.饿汉式:
class singleton
{
private singleton()
{
}
private static singleton instance=new singleton();
public static singleton getInstance()
{
return instance;
}
}
2.懒汉式 可能存在线程安全问题(解决方式:1.同步锁。2.静态内部类。)
class singleton1
{
private singleton1()
{
}
private static singleton1 instance=null;
public static singleton1 getinstance()
{
if(instance==null)
{
instance=new singleton1();
}
return instance;
}
}