zoukankan      html  css  js  c++  java
  • 几种单例模式

    1. 懒汉-线程不安全

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

      

    2. 懒汉-线程安全

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

      

    3. 饿汉-线程安全-内存占用较大,类加载时就创建了实例

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

      

    4. 双锁

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

      

  • 相关阅读:
    C#设计模式总结
    【23】备忘录模式(Memento Pattern)
    【22】访问者模式(Visitor Pattern)
    mycat 分库
    mysql的存储过程
    mysql的视图
    mysql的索引
    mysql权限操作
    mysql事务操作
    mysql常用函数
  • 原文地址:https://www.cnblogs.com/leavescy/p/12793376.html
Copyright © 2011-2022 走看看