——.NET设计模式系列之二
Terrylee
,
2005
年
12
月
07
日
概述
Singleton
模式要求一个类有且仅有一个实例,并且提供了一个全局的访问点。这就提出了一个问题:如何绕过常规的构造器,提供一种机制来保证一个类只有一个实例?客户程序在调用某一个类时,它是不会考虑这个类是否只能有一个实例等问题的,所以,这应该是类设计者的责任,而不是类使用者的责任。
从另一个角度来说,
Singleton
模式其实也是一种职责型模式。因为我们创建了一个对象,这个对象扮演了独一无二的角色,在这个单独的对象实例中,它集中了它所属类的所有权力,同时它也肩负了行使这种权力的职责!
意图
保证一个类仅有一个实例,并提供一个访问它的全局访问点。
模型图
逻辑模型图:
![](http://terrylee.cnblogs.com/images/cnblogs_com/terrylee/PIC025.jpg)
物理模型图:
生活中的例子
美国总统的职位是Singleton,美国宪法规定了总统的选举,任期以及继任的顺序。这样,在任何时刻只能由一个现任的总统。无论现任总统的身份为何,其头衔"美利坚合众国总统"是访问这个职位的人的一个全局的访问点。
![](http://terrylee.cnblogs.com/images/cnblogs_com/terrylee/PIC031.jpg)
五种实现
1
.简单实现
该实现方式主要有两个优点:
l
由于实例是在 Instance 属性方法内部创建的,因此类可以使用附加功能(例如,对子类进行实例化),即使它可能引入不想要的依赖性。
l
直到对象要求产生一个实例才执行实例化;这种方法称为“惰性实例化”。惰性实例化避免了在应用程序启动时实例化不必要的 singleton。
2
.安全的线程
这种方式的实现对于线程来说是安全的。我们首先创建了一个进程辅助对象,线程在进入时先对辅助对象加锁然后再检测对象是否被创建,这样可以确保只有一个实例被创建,因为在同一个时刻加了锁的那部分程序只有一个线程可以进入。这种情况下,对象实例由最先进入的那个线程创建,后来的线程在进入时(
instence == null
)为假,不会再去创建对象实例了。但是这种实现方式增加了额外的开销,损失了性能。
3
.双重锁定
这种实现方式对多线程来说是安全的,同时线程不是每次都加锁,只有判断对象实例没有被创建时它才加锁,有了我们上面第一部分的里面的分析,我们知道,加锁后还得再进行对象是否已被创建的判断。它
解决了线程并发问题,同时避免在每个
Instance
属性方法的调用中都出现独占锁定。它还允许您将实例化延迟到第一次访问对象时发生。实际上,应用程序很少需要这种类型的实现。大多数情况下我们会用静态初始化。
这种方式仍然有很多缺点:无法实现延迟初始化。
1
public
sealed
class
Singleton
2![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
3
staticSingletoninstance=null;
4
staticreadonlyobjectpadlock=newobject();
5![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
6
Singleton()
7![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
8
}
9![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
10
publicstaticSingletonInstance
11![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
12
get
13![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
14
if(instance==null)
15![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
16
lock(padlock)
17![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
18
if(instance==null)
19![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
20
instance=newSingleton();
21
}
22
}
23
}
24
returninstance;
25
}
26
}
27
}
28
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
2
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/b854634c0904529d4018c4c3336be836.gif)
3
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
4
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
5
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
6
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
7
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/b854634c0904529d4018c4c3336be836.gif)
8
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
9
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
10
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
11
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/b854634c0904529d4018c4c3336be836.gif)
12
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
13
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/b854634c0904529d4018c4c3336be836.gif)
14
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
15
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/b854634c0904529d4018c4c3336be836.gif)
16
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
17
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/b854634c0904529d4018c4c3336be836.gif)
18
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
19
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/b854634c0904529d4018c4c3336be836.gif)
20
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
21
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
22
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
23
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
24
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
25
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
26
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
27
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
28
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
4
.静态初始化
看到上面这段富有戏剧性的代码,我们可能会产生怀疑,这还是
Singleton
模式吗?
在此实现中,将在第一次引用类的任何成员时创建实例。公共语言运行库负责处理变量初始化。该类标记为 sealed 以阻止发生派生,而派生可能会增加实例。此外,变量标记为 readonly,这意味着只能在静态初始化期间(此处显示的示例)或在类构造函数中分配变量。
1
public
sealed
class
Singleton
2![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
3
staticreadonlySingletoninstance=newSingleton();
4![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
5
staticSingleton()
6![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
7
}
8![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
9
Singleton()
10![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
11
}
12![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
13
publicstaticSingletonInstance
14![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
15
get
16![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
17
returninstance;
18
}
19
}
20
}
21
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
2
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/b854634c0904529d4018c4c3336be836.gif)
3
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
4
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
5
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
6
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/b854634c0904529d4018c4c3336be836.gif)
7
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
8
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
9
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
10
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/b854634c0904529d4018c4c3336be836.gif)
11
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
12
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
13
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
14
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/b854634c0904529d4018c4c3336be836.gif)
15
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
16
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/b854634c0904529d4018c4c3336be836.gif)
17
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
18
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
19
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
20
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
21
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
该实现与前面的示例类似,不同之处在于它依赖公共语言运行库来初始化变量。它仍然可以用来解决 Singleton 模式试图解决的两个基本问题:全局访问和实例化控制。公共静态属性为访问实例提供了一个全局访问点。此外,由于构造函数是私有的,因此不能在类本身以外实例化 Singleton 类;因此,变量引用的是可以在系统中存在的唯一的实例。
由于 Singleton 实例被私有静态成员变量引用,因此在类首次被对 Instance 属性的调用所引用之前,不会发生实例化。
这种方法唯一的潜在缺点是,您对实例化机制的控制权较少。在 Design Patterns形式中,您能够在实例化之前使用非默认的构造函数或执行其他任务。由于在此解决方案中由 .NET Framework 负责执行初始化,因此您没有这些选项。在大多数情况下,静态初始化是在 .NET 中实现 Singleton 的首选方法。
5
.延迟初始化
这里,初始化工作有Nested类的一个静态成员来完成,这样就实现了延迟初始化,并具有很多的优势,是值得推荐的一种实
现方式。
现方式。
1
public
sealed
class
Singleton
2![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
3
Singleton()
4![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
5
}
6![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
7
publicstaticSingletonInstance
8![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
9
get
10![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
11
returnNested.instance;
12
}
13
}
14![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
15
classNested
16![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
17
staticNested()
18![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
19
}
20![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
21
internalstaticreadonlySingletoninstance=newSingleton();
22
}
23
}
24
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
2
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/b854634c0904529d4018c4c3336be836.gif)
3
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
4
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/b854634c0904529d4018c4c3336be836.gif)
5
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
6
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
7
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
8
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/b854634c0904529d4018c4c3336be836.gif)
9
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
10
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/b854634c0904529d4018c4c3336be836.gif)
11
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
12
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
13
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
14
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
15
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
16
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/b854634c0904529d4018c4c3336be836.gif)
17
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
18
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/b854634c0904529d4018c4c3336be836.gif)
19
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
20
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
21
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
22
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
23
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
24
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
实现要点
l Singleton模式是限制而不是改进类的创建。
l
可以很简单的修改一个
Singleton
,使它有少数几个实例,这样做是允许的而且是有意义的。
优点
l
实例控制:
Singleton
会阻止其他对象实例化其自己的 Singleton 对象的副本,从而确保所有对象都访问唯一实例
l
灵活性:因为类控制了实例化过程,所以类可以更加灵活修改实例化过程
缺点
l
开销:虽然数量很少,
但如果每次对象请求引用时都要检查是否存在类的实例,将仍然需要一些开销。可以通过使用静态初始化解决此问题,上面的五种实现方式中已经说过了。
l
可能的开发混淆:
使用
singleton
对象(尤其在类库中定义的对象)时,开发人员必须记住自己不能使用
new
关键字实例化对象。因为可能无法访问库源代码,因此应用程序开发人员可能会意外发现自己无法直接实例化此类。
l
对象的生存期:
Singleton
不能解决删除单个对象的问题。在提供内存管理的语言中(例如基于 .NET Framework 的语言),只有 Singleton 类能够导致实例被取消分配,因为它包含对该实例的私有引用。在某些语言中(如 C++),其他类可以删除
对象实例,但这样会导致 Singleton 类中出现悬浮引用。
对象实例,但这样会导致 Singleton 类中出现悬浮引用。
适用性
l
当类只能有一个实例而且客户可以从一个众所周知的访问点访问它时。
l
当这个唯一实例应该是通过子类化可扩展的,并且客户应该无需更改代码就能使用一个扩展的实例时。
应用场景
l
PC
机中可能有几个串口,但只能有一个
COM1
口的实例。
l
系统中只能有一个窗口管理器。
l
.NET Remoting
中服务器激活对象中的
Sigleton
对象,确保所有的客户程序的请求都只有一个实例来处理。
完整示例
这是一个简单的计数器例子,四个线程同时进行计数。
总结
Singleton
设计模式是一个非常有用的机制,可用于在面向对象的应用程序中提供单个访问点。文中通过五种实现方式的比较和一个完整的示例,完成了对
Singleton
模式的一个总结和探索。用一句广告词来概括
Singleton
模式就是“简约而不简单”。
_________________________________________________________________________________________________
源码下载:/Files/Terrylee/SigletonPattern.rar
源码下载:/Files/Terrylee/SigletonPattern.rar
参考文献:
《
C#
计模式》,中国电力出版社
使用
Microsoft .NET
的企业解决方案模式
1
using
System;
2
using
System.Threading;
3![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
4
namespace
SigletonPattern.SigletonCounter
5![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
6![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**////<summary>
7
///功能:简单计数器的单件模式
8
///编写:Terrylee
9
///日期:2005年12月06日
10
///</summary>
11
publicclassCountSigleton
12![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
13![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**////存储唯一的实例
14
staticCountSigletonuniCounter=newCountSigleton();
15![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
16![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**////存储计数值
17
privateinttotNum=0;
18![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
19
privateCountSigleton()
20![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
21![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
22![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**////线程延迟2000毫秒
23
Thread.Sleep(2000);
24
}
25![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
26
staticpublicCountSigletonInstance()
27![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
28![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
29![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
30
returnuniCounter;
31![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
32
}
33![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
34![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**////计数加1
35
publicvoidAdd()
36![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
37
totNum++;
38
}
39![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
40![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**////获得当前计数值
41
publicintGetCounter()
42![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
43
returntotNum;
44
}
45![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
46
}
47
}
48
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
2
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
3
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
4
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
5
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/b854634c0904529d4018c4c3336be836.gif)
6
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
7
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
8
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
9
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
10
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
11
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
12
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/b854634c0904529d4018c4c3336be836.gif)
13
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
14
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
15
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
16
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
17
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
18
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
19
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
20
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
21
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/b854634c0904529d4018c4c3336be836.gif)
22
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
23
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
24
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
25
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
26
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
27
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
28
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/b854634c0904529d4018c4c3336be836.gif)
29
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
30
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
31
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
32
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
33
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
34
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
35
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
36
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/b854634c0904529d4018c4c3336be836.gif)
37
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
38
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
39
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
40
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
41
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
42
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/b854634c0904529d4018c4c3336be836.gif)
43
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
44
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
45
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
46
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
47
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
48
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
1
using
System;
2
using
System.Threading;
3
using
System.Text;
4![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
5
namespace
SigletonPattern.SigletonCounter
6![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
7![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**////<summary>
8
///功能:创建一个多线程计数的类
9
///编写:Terrylee
10
///日期:2005年12月06日
11
///</summary>
12
publicclassCountMutilThread
13![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
14
publicCountMutilThread()
15![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
16![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
17
}
18![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
19![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**////<summary>
20
///线程工作
21
///</summary>
22
publicstaticvoidDoSomeWork()
23![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
24![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**////构造显示字符串
25
stringresults="";
26![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
27![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**////创建一个Sigleton实例
28
CountSigletonMyCounter=CountSigleton.Instance();
29![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
30![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**////循环调用四次
31
for(inti=1;i<5;i++)
32![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
33![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**////开始计数
34
MyCounter.Add();
35![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
36
results+="线程";
37
results+=Thread.CurrentThread.Name.ToString()+"——〉";
38
results+="当前的计数:";
39
results+=MyCounter.GetCounter().ToString();
40
results+="/n";
41![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
42
Console.WriteLine(results);
43![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
44![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**////清空显示字符串
45
results="";
46
}
47
}
48![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
49
publicvoidStartMain()
50![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
51![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
52
Threadthread0=Thread.CurrentThread;
53![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
54
thread0.Name="Thread0";
55![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
56
Threadthread1=newThread(newThreadStart(DoSomeWork));
57![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
58
thread1.Name="Thread1";
59![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
60
Threadthread2=newThread(newThreadStart(DoSomeWork));
61![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
62
thread2.Name="Thread2";
63![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
64
Threadthread3=newThread(newThreadStart(DoSomeWork));
65![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
66
thread3.Name="Thread3";
67![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
68
thread1.Start();
69![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
70
thread2.Start();
71![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
72
thread3.Start();
73![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
74![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**////线程0也只执行和其他线程相同的工作
75
DoSomeWork();
76
}
77
}
78
}
79
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
2
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
3
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
4
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
5
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
6
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/b854634c0904529d4018c4c3336be836.gif)
7
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
8
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
9
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
10
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
11
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
12
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
13
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/b854634c0904529d4018c4c3336be836.gif)
14
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
15
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/b854634c0904529d4018c4c3336be836.gif)
16
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
17
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
18
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
19
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
20
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
21
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
22
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
23
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/b854634c0904529d4018c4c3336be836.gif)
24
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
25
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
26
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
27
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
28
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
29
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
30
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
31
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
32
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/b854634c0904529d4018c4c3336be836.gif)
33
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
34
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
35
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
36
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
37
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
38
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
39
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
40
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
41
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
42
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
43
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
44
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
45
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
46
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
47
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
48
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
49
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
50
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/b854634c0904529d4018c4c3336be836.gif)
51
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
52
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
53
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
54
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
55
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
56
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
57
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
58
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
59
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
60
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
61
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
62
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
63
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
64
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
65
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
66
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
67
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
68
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
69
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
70
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
71
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
72
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
73
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
74
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
75
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
76
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
77
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
78
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
79
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
1
using
System;
2
using
System.Text;
3
using
System.Threading;
4![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
5
namespace
SigletonPattern.SigletonCounter
6![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
7![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**////<summary>
8
///功能:实现多线程计数器的客户端
9
///编写:Terrylee
10
///日期:2005年12月06日
11
///</summary>
12
publicclassCountClient
13![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
14
publicstaticvoidMain(string[]args)
15![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
16
CountMutilThreadcmt=newCountMutilThread();
17![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
18
cmt.StartMain();
19![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
20
Console.ReadLine();
21
}
22
}
23
}
24
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
2
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
3
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
4
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
5
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
6
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/b854634c0904529d4018c4c3336be836.gif)
7
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
8
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
9
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
10
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
11
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
12
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
13
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/b854634c0904529d4018c4c3336be836.gif)
14
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
15
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/b854634c0904529d4018c4c3336be836.gif)
16
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
17
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
18
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
19
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
20
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
21
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
22
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
23
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
24
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
l Singleton类中的实例构造器可以设置为Protected以允许子类派生。
l Singleton模式一般不要支持Icloneable接口,因为这可能导致多个对象实例,与Singleton模式的初衷违背。
l Singleton模式一般不要支持序列化,这也有可能导致多个对象实例,这也与Singleton模式的初衷违背。
l Singleton只考虑了对象创建的管理,没有考虑到销毁的管理,就支持垃圾回收的平台和对象的开销来讲,我们一般没必要对其销毁进行特殊的管理。
l 理解和扩展Singleton模式的核心是“如何控制用户使用new对一个类的构造器的任意调用”。
1
public
sealed
class
Singleton
2![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
3
staticSingletoninstance=null;
4
staticreadonlyobjectpadlock=newobject();
5![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
6
Singleton()
7![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
8
}
9![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
10
publicstaticSingletonInstance
11![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
12
get
13![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
14
lock(padlock)
15![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
16
if(instance==null)
17![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
18
instance=newSingleton();
19
}
20
returninstance;
21
}
22
}
23
}
24
}
25![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
26
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
2
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/b854634c0904529d4018c4c3336be836.gif)
3
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
4
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
5
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
6
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
7
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/b854634c0904529d4018c4c3336be836.gif)
8
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
9
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
10
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
11
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/b854634c0904529d4018c4c3336be836.gif)
12
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
13
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/b854634c0904529d4018c4c3336be836.gif)
14
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
15
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/b854634c0904529d4018c4c3336be836.gif)
16
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
17
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/b854634c0904529d4018c4c3336be836.gif)
18
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
19
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
20
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
21
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
22
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
23
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
24
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
25
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
26
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
1
public
sealed
class
Singleton
2![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
3
staticSingletoninstance=null;
4![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
5
Singleton()
6![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
7
}
8![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
9
publicstaticSingletonInstance
10![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
11
get
12![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
13
if(instance==null)
14![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
15
instance=newSingleton();
16
}
17
returninstance;
18
}
19
}
20
}
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
2
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/b854634c0904529d4018c4c3336be836.gif)
3
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
4
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
5
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
6
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/b854634c0904529d4018c4c3336be836.gif)
7
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
8
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
9
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
10
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/b854634c0904529d4018c4c3336be836.gif)
11
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
12
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/b854634c0904529d4018c4c3336be836.gif)
13
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
14
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/b854634c0904529d4018c4c3336be836.gif)
15
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
16
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
17
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
18
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
19
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
20
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
这种方式的实现对于线程来说并不是安全的,因为在多线程的环境下有可能得到Singleton类的多个实例。如果同时有两个线程去判断(instance == null),并且得到的结果为真,这时两个线程都会创建类Singleton的实例,这样就违背了Singleton模式的原则。实际上在上述代码中,有可能在计算出表达式的值之前,对象实例已经被创建,但是内存模型并不能保证对象实例在第二个线程创建之前被发现。