zoukankan      html  css  js  c++  java
  • 单例设计模式Singleton之懒加载模式(懒汉模式)【原】

    单例设计模式Singleton之懒加载模式(懒汉模式)

    SingletonLazy.java类

    package kingtool;
    
    import kingtool.http.IPTool;
    
    
    public class SingletonLazy {
        // 懒汉式单例模式
        // 比较懒,在类加载时,不创建实例,因此类加载速度快,但运行时获取对象的速度慢(第一次)
        private static SingletonLazy intance = null;// 静态私用成员,没有初始化
    
        
        private String currentIp = "";
        private SingletonLazy() {//所有加载一次的其它类代码都放在这里
            currentIp = IPTool.judgeIP();//其它代码,仅加载一次用
        }
    
        
        public static synchronized SingletonLazy getInstance() // 静态,同步,公开访问点
        {
            if (intance == null) {
                synchronized (SingletonLazy.class) {
                    if (intance == null) {
                        intance = new SingletonLazy();
                    }
                }
            }
            return intance;
        }
    
    
        public String getCurrentIp() {
            return currentIp;
        }
    
        public static void main(String[] args) {
            SingletonLazy single = new SingletonLazy();
            single = new SingletonLazy();
            single = new SingletonLazy();
            single = new SingletonLazy();
            single.getCurrentIp();
        }
    }
  • 相关阅读:
    名字空间,L,E, G , B 作用域, 内置电池
    lambda表达式
    表达式与声明的区别。
    jupyter book的使用
    centos7一键安装cacti_1.2.16版本
    docker修改阿里云镜像加速器
    centos单网卡多ip,被动模式
    centos同步时间
    centos7.x制作bond
    centos 6.X制作bond
  • 原文地址:https://www.cnblogs.com/whatlonelytear/p/6306414.html
Copyright © 2011-2022 走看看