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

     1

    using System;

    public class Singleton
    {
       private static Singleton instance;

       private Singleton() {}

       public static Singleton Instance
       {
          get 
          {
             if (instance == null)
             {
                instance = new Singleton();
             }
             return instance;
          }
       }
    }

    2


    public sealed class Singleton
    {
       private static readonly Singleton instance = new Singleton();
       
       private Singleton(){}

       public static Singleton Instance
       {
          get 
          {
             return instance; 
          }
       }
    }

    3

    using System;

    public sealed class Singleton
    {
       private static volatile Singleton instance;
       private static object syncRoot = new Object();

       private Singleton() {}

       public static Singleton Instance
       {
          get 
          {
             if (instance == null
             {
                lock (syncRoot) 
                {
                   if (instance == null
                      instance = new Singleton();
                }
             }

             return instance;
          }
       }
    }
  • 相关阅读:
    一则自用iptables例子解释
    SVN配置钩子文件限制提交文件时必须填写更新日志
    Nginx反向代理配置配置实例
    mysql 物理数据存放
    VisualVM、JConsole
    Rabbitmq
    pdm画表间结构
    tomcat jvm 参数优化
    【转载】Java导入导出excel
    【转载】使用 Google Guava 美化你的 Java 代码
  • 原文地址:https://www.cnblogs.com/cr7/p/2294053.html
Copyright © 2011-2022 走看看