1.饿汉式
class A{
private A(){}
private static A a = new A();
public static A getA(){
return a;
}
}
2.懒汉式
class B{
private B() { }
private static B b;
private static B getB(){
if(b==null){
b = new B();
}
return b;
}
}
3.线程安全的懒汉式
class C{
private C(){}
private static volatile C c;
private static synchronized C getC(){
if(c==null){
synchronized (C.class){
c = new C();
}
}
return c;
}
}