the first type (lazy, thread insecure):public class Singleton {private static Singleton instance; Private Singleton () {} public static Singleton getinstance () {if (instance = = null) {instance = new single ton (); } return instance; }}the second type (lazy, thread safe):public class Singleton {private static Singleton instance; Private Singleton () {} public static synchronized Singleton getinstance () {if (instance = null) {instance = new Singleton (); } return instance; }}The third type (a Hungry man):public class Singleton {private static Singleton instance = new Singleton (); Private Singleton () {} public static Singleton getinstance () {return instance; }}the fourth type ( hungry Han, variant):public class Singleton {private Singleton instance = null; static {instance = new Singleton (); } private Singleton () {} public static Singleton getinstance () {return this.instance; }}Fifth type (Common DCL):public class Singleton {private static Singleton Singleton; Private Singleton () {} public static Singleton Getsingleton () {if (Singleton = = null) {synchronized (single Ton.class) {if (singleton = = null) {singleton = new Singleton (); }}} return singleton; }}Sixth type (DCL):public class Singleton {private volatile static Singleton Singleton; Private Singleton () {} public static Singleton Getsingleton () {if (Singleton = = null) {synchronized (single Ton.class) {if (singleton = = null) {singleton = new Singleton (); }}} return singleton; }}seventh Type (DCL):public class Singleton {private static Singleton Singleton; Private Singleton () {} public static Singleton Getsingleton () {if (Singleton = = null) {synchronized (single Ton.class) {if (singleton = = null) {Singleton TMP = new Singleton (); singleton = tmp; }}} return singleton; }}The eighth type:public class Singleton {private static class singletonholder{static final Singleton INSTANCE = new Singleto N (); } public static Singleton Getsingleton () {return singletomholder.instance; } }}
Single-Instance pattern notation