zoukankan      html  css  js  c++  java
  • 单例模式实现

    1. 静态内部类(static nested class) 优先考虑
    2.  
    3. public class LazySingleton{  
    4.     private LazySingleton(){}  
    5.   
    6.     private static class Nested{  
    7.         private static final LazySingleton single = new LazySingleton();  
    8.     }  
    9.   
    10.     public static LazySingleton getInstance(){  
    11.         return Nested.single;  
    12.     }  
    13. }  

    双重检查锁定(DCL)

    1. public class LazySingleton{  
    2.     private LazySingleton(){}  
    3.   
    4.     private static volatile LazySingleton single = null;  
    5.   
    6.     public static LazySingleton getInstance(){  
    7.         if(single == null){  
    8.             synchronized (LazySingleton.class){  
    9.                 if(single == null){  
    10.                     single = new LazySingleton();   //① 非原子操作  
    11.                 }  
    12.             }  
    13.         }   
    14.         return single;  
    15.     }  
    16. }  
  • 相关阅读:
    使用cwRsync在Windows的目录之间增量同步文件
    Linux搭建lnmp环境
    ie下文件上传无权访问的问题
    10
    8
    9
    7
    网络爬虫环境配置之的模块安装
    pip的更新问题
    【转】进程与线程的一个简单解释
  • 原文地址:https://www.cnblogs.com/sjqq/p/7486807.html
Copyright © 2011-2022 走看看