zoukankan      html  css  js  c++  java
  • 单例模式的多种实现方式

    //需要时加锁,双重校验
    class CustomSingleton{
    
       private static CustomSingleton customSingleton = null;
    
       private CustomSingleton(){};
    
       public static CustomSingleton getCustomSingleton(){
          if(customSingleton==null){
             synchronized (CustomSingleton.class) {
                if(customSingleton==null) {
                   customSingleton = new CustomSingleton();
                }
             }
          }
          return customSingleton;
       }
    }
    
    /*
    //需要时加锁,有并发问题
    class CustomSingleton{
    
       private static CustomSingleton customSingleton = null;
    
       private CustomSingleton(){};
    
       public static CustomSingleton getCustomSingleton(){
          if(customSingleton==null){
             synchronized (CustomSingleton.class) {
                customSingleton = new CustomSingleton();
             }
          }
          return customSingleton;
       }
    }*/
    
    /*
    //不加锁,有并发问题
    class CustomSingleton{
    
       private static CustomSingleton customSingleton = null;
    
       private CustomSingleton(){};
    
       public static CustomSingleton getCustomSingleton(){
          if(customSingleton==null){
             customSingleton = new CustomSingleton();
          }
          return customSingleton;
       }
    }*/
    
    /*
    //暴力同步锁
    class CustomSingleton{
    
       private static CustomSingleton customSingleton = null;
    
       private CustomSingleton(){};
    
       public static synchronized CustomSingleton getCustomSingleton(){
          if(customSingleton==null){
             customSingleton = new CustomSingleton();
          }
          return customSingleton;
       }
    }*/
    
    /*
    //线程安全  常量初始化
    class CustomSingleton{
    
       private static CustomSingleton customSingleton = new CustomSingleton();
    
       private CustomSingleton(){};
    
       public static CustomSingleton getCustomSingleton(){ return customSingleton;}
    }
    */
    
    /*
    //枚举单例
    enum CustomSingleton{
    
       SINGLETON;
    
       public void print(){
          System.out.println(123);
       }
    }*/
  • 相关阅读:
    CRM安装过程问题总结
    SQL Server Active Directory Helper 无法启动
    CRM导出Excel记录的最大数量
    CRM名词解释
    CRM根据不同的角色过滤视图
    asp.net C# webservice安全性方案
    利用MSCRM4.0 Trace功能跟踪详细错误信息
    事件1058处理过程,处理组策略失败.
    在 Windows Server 2003 中配置网络负载平衡
    对比SQL中简单嵌套查询与非嵌套查询的异同
  • 原文地址:https://www.cnblogs.com/gavinYang/p/11202246.html
Copyright © 2011-2022 走看看