zoukankan      html  css  js  c++  java
  • 2020-03-04

    庚子鼠年 戊寅月 丙午日

    描述

    把昨天的项目完成了,有时间在升级吧

    设计模式之单例模式

    技术博客:null

    随笔

    单例模式

    1. 构造方法私有化
    2. 对外提供获取实例的静态方法

    饿汉式

    方式一:静态属性 可以使用

    public class Singleton {
    
        private final static Singleton INSTANCE = new Singleton();
    
        private Singleton(){}
    
        public static Singleton getInstance(){
            return INSTANCE;
        }
    }
    

    方式二:静态代码块 可以使用

    public class Singleton {
    
        private static Singleton instance;
    
        static {
            instance = new Singleton();
        }
    
        private Singleton() {}
    
        public static Singleton getInstance() {
            return instance;
        }
    }
    

    懒汉式

    方式一:线程不安全 不可以使用

    public class Singleton {  
        private static Singleton instance;  
        private Singleton (){}  
      
        public static Singleton getInstance() {  
      	 	if (instance == null) {  
       		    instance = new Singleton();  
      		}  
        	return instance;  
        }  
    }
    

    方式二:(线程安全,同步方法) 效率不行,不推荐使用

    public class Singleton {
    
        private static Singleton singleton;
    
        private Singleton() {}
    
        public static synchronized Singleton getInstance() {
            if (singleton == null) {
                singleton = new Singleton();
            }
            return singleton;
        }
    }
    

    方式三:线程不安全,不可以使用

    public class Singleton {
    
        private static Singleton singleton;
    
        private Singleton() {}
    
        public static Singleton getInstance() {
            if (singleton == null) {
                synchronized (Singleton.class) {
                    singleton = new Singleton();
                }
            }
            return singleton;
        }
    }
    

    双重检查

    线程安全, 效率高 推荐使用

    public class Singleton {
    
        private static volatile Singleton singleton;
    
        private Singleton() {}
    
        public static Singleton getInstance() {
            if (singleton == null) {
                synchronized (Singleton.class) {
                    if (singleton == null) {
                        singleton = new Singleton();
                    }
                }
            }
            return singleton;
        }
    }
    

    静态内部类

    由于类加载的时候内部类不会加载,使用时才会去加载

    java虚拟机保证了类的加载是线程安全推荐使用

    public class Singleton {
    
        private Singleton() {}
    
        private static class SingletonInstance {
            private static final Singleton INSTANCE = new Singleton();
        }
    
        public static Singleton getInstance() {
            return SingletonInstance.INSTANCE;
        }
    }
    

    枚举

    借助JDK1.5中添加的枚举来实现单例模式。不仅能避免多线程同步问题,而且还能防止反序列化重新创建新的对象。可能是因为枚举在JDK1.5中才添加。

    public enum Singleton {
        INSTANCE;
        public void whateverMethod() {
    
        }
    }
    
  • 相关阅读:
    some requirement checks failed
    FTP下载文件时拒绝登陆申请怎么办?
    Linux查看与设定别名
    如何编写shell脚本
    Linux shell是什么
    Linux命令大全之查看登陆用户信息
    Linux命令大全之挂载命令
    论第二次作业之输入输出格式怎么合格(才疏学浅说的不对轻点喷我)
    文件词数统计
    软件工程作业--第一周
  • 原文地址:https://www.cnblogs.com/chang1024/p/12416365.html
Copyright © 2011-2022 走看看