参考链接:https://www.cnblogs.com/Ycheng/p/7169381.html
1、懒汉--线程不安全模式
public class SingletanceDemo01{ private static SingletanceDemo01 instance; private SingletanceDemo01(){} public static SingletanceDemo01 getInstance(){ if(instance == null){ instance = new SingletanceDemo01(); } return instance; } }
2、懒汉--线程安全
public class SingletanceInstance{ private static SingletanceInstance instance; private SingletanceInstance(){} public static synchronized SingletanceInstance getInstance(){ if(instance == null){ instance = new SingletanceInstance(); } return instance; } }
3、饿汉
public class SingletonDemo{ private static SingletonDemo instance = new SingletonDemo(); private SingletonDemo(){} public static SingletonDemo getInstance(){ return instance; } }