zoukankan      html  css  js  c++  java
  • 单例设计模式(懒汉式)


    单例设计模式
    确保一个类只有一个对象
    懒汉式 double checking
     1、构造器私有化,避免外部直接创建对象
     2、声明一个私有的静态变量
     3、创建一个对外的公共的静态方法 访问该变量,如果变量没有对象,创建该对象

    SynDemo01.java

    package cn.Thread02;
    /*
     * 单例设计模式:确保一个类只有一个对象
     */
    public class SynDemo01 {
        public static void main(String[] args) {
            JvmThread thread1=new JvmThread(100);
            JvmThread thread2=new JvmThread(500);
            thread1.start();
            thread2.start();
        }
    }
    class JvmThread extends Thread{
        private long time;
        public JvmThread() {
            
        }
        public JvmThread(long time) {
            this.time=time;
        }
        public void run() {
            System.out.println(Thread.currentThread().getName()+"-->创建    :"+Jvm.getInstance3(time));
        };
    }
    /*
     * 单例设计模式
     * 确保一个类只有一个对象
     * 懒汉式   double checking
     * 1、构造器私有化,避免外部直接创建对象
     * 2、声明一个私有的静态变量
     * 3、创建一个对外的公共的静态方法  访问该变量,如果变量没有对象,创建该对象
     */
    class Jvm{
        //声明一个私有的静态变量
        private static Jvm instance =null;
        //构造器私有化 ,避免外部直接创建对象
        private Jvm(){
            
        }
        //创建一个对外的公共的静态方法  访问该变量,如果变量没有对象,创建该对象
        
        public static  Jvm getInstance3(long time) {     //同步块方式优化
            //c d e 看到有对象 就进不去了 直接返回  提高效率。
            if(null==instance) {
                //a,b
                synchronized(Jvm.class) {
                    if(null==instance) {
                        try {
                            Thread.sleep(time);   //放大错误
                        } catch (InterruptedException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                        instance=new Jvm();
                    }
                    
                }
            }
            return instance;
        }
        
        
        public static  Jvm getInstance2(long time) {     //同步块方式
            synchronized(Jvm.class) {
                if(null==instance) {
                    try {
                        Thread.sleep(time);   //放大错误
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    instance=new Jvm();
                }
                return instance;
            }
        }
        
        
        public static synchronized Jvm getInstance(long time) {     //直接同步
            if(null==instance) {
                try {
                    Thread.sleep(time);   //放大错误
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                instance=new Jvm();
            }
            return instance;
        }
        
        
        public static  Jvm getInstance1(long time) {     //没有同步,会出现不同的对象地址
            if(null==instance) {
                try {
                    Thread.sleep(time);   //放大错误
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                instance=new Jvm();
            }
            return instance;
        }
    }
  • 相关阅读:
    properties to json (通过前缀手动创建json, bean) propsutils
    Drill 常用时间函数 drill
    ubuntu20.04 修改 DNS  ip
    javascript 获取图片的尺寸 how to get image size using javascript
    javascript小数点后保留N位并可以四舍五入
    C# 递归算法求 1,1,2,3,5,8,13···
    自加入屠龙后的成长记
    Session丢值的问题
    第二个网站成长经历,http://www.chaomagou.com/ 潮妈购
    回想自己2012年1月1日到2012年6月19日的所作所为
  • 原文地址:https://www.cnblogs.com/ssxblog/p/11254087.html
Copyright © 2011-2022 走看看