标签:线程安全 size ble ora 介绍 str none static position
public class Singleton1 { private static Singleton1 singleton1 = new Singleton1(); private Singleton1() { } public static Singleton1 getSingleton() { return singleton1; } }
public class Singleton5 { private static Singleton5 singleton5; private Singleton5() { } static { singleton5 = new Singleton5(); } public static Singleton5 getSingleton() { return singleton5; } }
public class Singleton2 { private static Singleton2 singleton2; private Singleton2() { } public static synchronized Singleton2 getSingleton() { if(singleton2==null) { singleton2 = new Singleton2(); } return singleton2; } }
public class Singleton3 { private static class Singleton{ public static Singleton3 singleton= new Singleton3(); } private Singleton3() { } public static Singleton3 getSingleton() { return Singleton.singleton; } }
public class Singleton3 implements Serializable {
private static final long serUID = 888L;
private static class Singleton{
public static Singleton3 singleton= new Singleton3();
}
private Singleton3() {
}
public static Singleton3 getSingleton() {
return Singleton.singleton;
}
protected Object readResolve() throws ObjectStreamException {
return Singleton.singleton;
}
}
public enum Singleton6 { singleton6; private Singleton6(){ } public Singleton6 getSingleton(){ return singleton6; } }
public class Singleton4 { private volatile static Singleton4 singleton4; private Singleton4() { } public static Singleton4 getSingleton() { if(singleton4==null) { synchronized (Singleton4.class) { singleton4 = new Singleton4(); } } return singleton4; } }
标签:线程安全 size ble ora 介绍 str none static position
原文地址:https://www.cnblogs.com/use-D/p/9757746.html