1. //饿汉模式
    2. public class HurrySingleton {
    3. private static final HurrySingleton INSTANCE = new HurrySingleton();
    4. //私有化构造子,阻止外部直接实例化对象
    5. private HurrySingleton(){
    6. }
    7. /**
    8. * 获取类的单例实例
    9. */
    10. public static HurrySingleton getInstance(){
    11. return INSTANCE;
    12. }
    13. }
    14. // 懒汉单例模式
    15. public class LazySignleton {
    16. private static LazySignleton INSTANCE = null;
    17. // 私有化构造子,阻止外部直接实例化对象
    18. private LazySignleton(){
    19. }
    20. // 获取类的单例实例
    21. public static LazySignleton getInstance(){
    22. if(INSTANCE == null){
    23. synchronized (LazySignleton.class) {
    24. if(INSTANCE == null){
    25. INSTANCE = new LazySignleton();
    26. }
    27. }
    28. }
    29. return INSTANCE;
    30. }
    31. }