zoukankan      html  css  js  c++  java
  • 单件模式

    代码
    using System;
    using System.Collections.Generic;

    /// <summary>
    /// 单件模式(线程安全的单件(双重锁定方法2:线程安全而且性能优越))
    /// </summary>
    public class Singleton
    {
        
    /// <summary>
        
    /// 定义自身静态变量实例默认为null
        
    /// </summary>
        private static Singleton instance=null;
        
        
    private static readonly object olock=new Object();
        
    ///私有构造函数
        
    /// 之所以不为public类型,因为是为了避免发生new Singleton()产生实例
        private Singleton()
        {}
        
    public static Singleton getInstance()
        {
            
    if(instance==null)
            {
                
    lock(olock)
                {
                    
    if(instance==null)
                    {
                        instance
    =new Singleton();
                    }
                    
    return instance;
                }
            }
        }
    }

     1         public static Singleton GetInstance()
     2         {
     3             if (singleton == null)
     4             {
     5                 lock (syncObject)
     6                 {
     7 
     8                     if (singleton == null)
     9                     {
    10                         singleton = new Singleton();
    11                     }
    12                 }
    13             }
    14             return singleton;
    15         } 
     1 namespace Singleton
     2 {
     3     public sealed class Singleton
     4     {
     5         private static readonly Singleton singleton = new Singleton();
     6 
     7         private Singleton()
     8         {
     9         }
    10 
    11         public static Singleton GetInstance()
    12         {
    13             return singleton;
    14         }
    15     }
    16 }



  • 相关阅读:
    从零入门 Serverless | Serverless Kubernetes 应用部署及扩缩容
    从单体迈向 Serverless 的避坑指南
    从零入门 Serverless | 教你使用 IDE/Maven 快速部署 Serverless 应用
    开发函数计算的正确姿势——OCR 服务
    从零入门 Serverless | 函数计算的开发与配置
    全部满分!阿里云函数计算通过可信云21项测试
    登录接口+三方登录 微博
    注册接口文档
    异步发送短信验证与 注册接口完善
    图片验证码接口
  • 原文地址:https://www.cnblogs.com/mikechang/p/1705077.html
Copyright © 2011-2022 走看看