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;
        }
    }
  • 相关阅读:
    小白扫盲之-计算机为何需要内存
    Centos 安装Pycharm 并移动到桌面。
    Docker守护进程
    插入排序
    快速排序
    归并排序
    __metaclass__方法
    Python面向对象(2)类空间问题以及类之间的关系
    Python面向对象(1)_初步认识
    python语法基础(8)_包
  • 原文地址:https://www.cnblogs.com/ssxblog/p/11254087.html
Copyright © 2011-2022 走看看