惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

C
CXSECURITY Database RSS Feed - CXSecurity.com
Stack Overflow Blog
Stack Overflow Blog
月光博客
月光博客
T
Threat Research - Cisco Blogs
小众软件
小众软件
有赞技术团队
有赞技术团队
酷 壳 – CoolShell
酷 壳 – CoolShell
Apple Machine Learning Research
Apple Machine Learning Research
C
Cyber Attacks, Cyber Crime and Cyber Security
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
T
Tailwind CSS Blog
Cisco Talos Blog
Cisco Talos Blog
V
V2EX
博客园 - 【当耐特】
C
Cybersecurity and Infrastructure Security Agency CISA
Hugging Face - Blog
Hugging Face - Blog
The Cloudflare Blog
The Last Watchdog
The Last Watchdog
Simon Willison's Weblog
Simon Willison's Weblog
T
Threatpost
S
Secure Thoughts
O
OpenAI News
P
Proofpoint News Feed
S
SegmentFault 最新的问题
Forbes - Security
Forbes - Security
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Application and Cybersecurity Blog
Application and Cybersecurity Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Last Week in AI
Last Week in AI
宝玉的分享
宝玉的分享
Scott Helme
Scott Helme
T
Tenable Blog
A
Arctic Wolf
L
LINUX DO - 热门话题
爱范儿
爱范儿
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
www.infosecurity-magazine.com
www.infosecurity-magazine.com
V
Visual Studio Blog
Hacker News: Ask HN
Hacker News: Ask HN
Hacker News - Newest:
Hacker News - Newest: "LLM"
腾讯CDC
博客园 - Franky
WordPress大学
WordPress大学
Know Your Adversary
Know Your Adversary
博客园_首页
雷峰网
雷峰网
IT之家
IT之家
PCI Perspectives
PCI Perspectives
L
LINUX DO - 最新话题
H
Heimdal Security Blog

博客园 - Nillson

设计模式--简单工厂模式 策略模式 抽象类与接口 C# 实现的一个二叉树类 回顾一个面试题 再谈代理 常见的排序方法 预定义,宏定义 连接符,数值运算与函数 复杂查询 数据库中的Index和View的理解 重载和重写 采用递归的方法获得一棵树的所有叶节点 .NET中的新概念整理 4月要看的书 System.Runtime.InteropServices浅见 挂个牛人 一篇关于如何写注释的文章,值得收藏 Vistual Studio 2005到Vistual Studio 2008的版本转换问题 Visual Studio 2008 的一个Bug
传说中的Singleton....
Nillson · 2010-06-03 · via 博客园 - Nillson

Posted on 2010-06-03 18:22  Nillson  阅读(260)  评论()    收藏  举报

最近做了一些WCF的项目,在客户端利用ChannelFactory创建一个Channel时会希望这个ServiceChannel在整个客户端中是唯一的。于是乎用了以下的代码,并一度为自己的聪明才智而欢欣鼓舞

代码

 1         /// <summary>
 2         /// service for Boss service action
 3         /// </summary>
 4         private IEBService _service;
 5 
 6         /// <summary>
 7         /// channel factories wich can create a channel for IEBService
 8         /// </summary>
 9         private ChannelFactory<IEBService> _channelFactory;
10 
11         public IEBService Service
12         {
13             get
14             {
15                 if (_service == null)
16                 {
17                     BasicHttpBinding binding = new BasicHttpBinding();
18                     binding.MaxReceivedMessageSize = EBSettings.MessageCapacity;
19                     binding.ReaderQuotas.MaxArrayLength = EBSettings.MessageCapacity;
20                     binding.SendTimeout = TimeSpan.FromHours(1);
21                     binding.ReceiveTimeout = TimeSpan.FromHours(1);
22                     string serviceUrl = string.Format(EBSettings.EBServiceURLPattern, MachineName, EBSettings.EBServicePort);
23                     this._channelFactory = new ChannelFactory<IEBService>(binding, serviceUrl);
24                     _service = _channelFactory.CreateChannel();
25                 }
26                 return _service;
27             }
28         }
29 

后来才知道这就是传说中的Singleton 模式 ~~。更可悲的是我曾亲口对一个哥们说过:”Singleton模式?什么东西?“ 看来人不能傻到这种程度....

补课>>

代码

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace ConsoleApplication1
 7 {
 8     class Program
 9     {
10         static void Main(string[] args)
11         {
12             SingletonClass.Instance.Print(); // The way to call the instance
13         }
14     }
15 
16     class SingletonClass
17     {
18         private SingletonClass()
19         {
20             // do nothing but override the contribute method to avoid new an instant outside this class
21         }
22 
23         private static SingletonClass _instance = null;
24 
25         /// <summary>
26         /// The only instance that use to return
27         /// </summary>
28         public static SingletonClass Instance
29         {
30             // Client can only access from here and this will make sure the same SingletonClass instance be returned
31             get
32             {
33                 if (_instance == null)
34                 {
35                     _instance = new SingletonClass();
36                 }
37                 return _instance;
38             }
39             
40             private set
41             {
42             }
43         }
44 
45         public void Print()
46         {
47             Console.WriteLine("I'm the only instance of Singleton Class");
48         }
49     }
50 }
51 

然而据可靠资料(MSDN)显示,这种做法在遇到多线程的时候会出现问题,即在当两个线程同时尝试创建该类的实例时,他们的初始条件(_instance == null)可能都符合,所以每个线程会试图去创建一个该类的实例。这种情况肯定是咱家不希望看到的,但是具体会出现啥样的情况需要以实践来检验。对此MSDN没有给出官方的说法~~,但是一种多线程下的Singleton模式也由此诞生了

代码

 1 using System;
 2 
 3 public sealed class Singleton
 4 {
 5    private static volatile Singleton instance;
 6    private static object syncRoot = new Object();
 7 
 8    private Singleton() {}
 9 
10    public static Singleton Instance
11    {
12       get 
13       {
14          if (instance == null
15          {
16             lock (syncRoot) 
17             {
18                if (instance == null
19                   instance = new Singleton();
20             }
21          }
22 
23          return instance;
24       }
25    }
26 }
27 

这段代码在实例化之前加了一把”锁“让跑在它后边的线程暂时进不来,也就解决了上一段代码所可能存在的问题。

官方资料给的很清楚,不翻译了

 http://msdn.microsoft.com/en-us/library/ff650316.aspx