关于Singleton设计模式的计数器代码实例(拷贝粘贴即可学习)
2005-09-12 00:22
806 查看
Counter.cs:
class Counter
{
//存储唯一的实例
static Counter uniCounter = new Counter();
//存储计数值。
private int totNum = 0;
//用private访问修饰符是防止用new关键字来实例化本类
private Counter()
{
//这里假设因为某种因素而耽搁了100毫秒
Thread.Sleep(100);
}
static public Counter instance()
{
return uniCounter;
}
//计数加1
public void Inc()
{
totNum ++;
}
//获得当前计数值
public int GetCounter()
{
return totNum;
}
}
Couter的客户端程序:
class MutileClient
{
public MutileClient()
{
}
public void DoSomeWork()
{
Counter myCounter = Counter.instance();
for (int i = 1; i < 5; i++)
{
myCounter.Inc();
Console.WriteLine("线程{0}报告: 当前counter为: {1}", Thread.CurrentThread.Name.ToString(), myCounter.GetCounter().ToString());
}
}
public void ClientMain()
{
Thread thread0 = Thread.CurrentThread;
thread0.Name = "Thread 0";
Thread thread1 = new Thread(new ThreadStart(this.DoSomeWork));
thread1.Name = "Thread 1";
Thread thread2 = new Thread(new ThreadStart(this.DoSomeWork));
thread2.Name = "Thread 2";
Thread thread3 = new Thread(new ThreadStart(this.DoSomeWork));
thread3.Name = "Thread 3";
thread1.Start();
thread2.Start();
thread3.Start();
//线程0也只执行和其他线程相同的工作。
DoSomeWork();
}
}
测试类:
class App
{
public static void Main()
{
//测试有关Singleton方面的Counter
MutileClient myClient = new MutileClient();
myClient.ClientMain();
System.Console.ReadLine();
}
}
class Counter
{
//存储唯一的实例
static Counter uniCounter = new Counter();
//存储计数值。
private int totNum = 0;
//用private访问修饰符是防止用new关键字来实例化本类
private Counter()
{
//这里假设因为某种因素而耽搁了100毫秒
Thread.Sleep(100);
}
static public Counter instance()
{
return uniCounter;
}
//计数加1
public void Inc()
{
totNum ++;
}
//获得当前计数值
public int GetCounter()
{
return totNum;
}
}
Couter的客户端程序:
class MutileClient
{
public MutileClient()
{
}
public void DoSomeWork()
{
Counter myCounter = Counter.instance();
for (int i = 1; i < 5; i++)
{
myCounter.Inc();
Console.WriteLine("线程{0}报告: 当前counter为: {1}", Thread.CurrentThread.Name.ToString(), myCounter.GetCounter().ToString());
}
}
public void ClientMain()
{
Thread thread0 = Thread.CurrentThread;
thread0.Name = "Thread 0";
Thread thread1 = new Thread(new ThreadStart(this.DoSomeWork));
thread1.Name = "Thread 1";
Thread thread2 = new Thread(new ThreadStart(this.DoSomeWork));
thread2.Name = "Thread 2";
Thread thread3 = new Thread(new ThreadStart(this.DoSomeWork));
thread3.Name = "Thread 3";
thread1.Start();
thread2.Start();
thread3.Start();
//线程0也只执行和其他线程相同的工作。
DoSomeWork();
}
}
测试类:
class App
{
public static void Main()
{
//测试有关Singleton方面的Counter
MutileClient myClient = new MutileClient();
myClient.ClientMain();
System.Console.ReadLine();
}
}
相关文章推荐
- 关于设计模式的学习
- 深入浅出单实例SINGLETON设计模式
- 深入浅出单实例Singleton设计模式
- 跟着实例学习设计模式(2)-静态工厂(创建型)
- 深入浅出单实例Singleton设计模式
- 设计模式实例学习-责任链模式
- 深入浅出单实例Singleton设计模式
- C# 设计模式学习三 Abstract Factory 抽象工厂(实例)
- 设计模式实例与代码:Builder模式
- 设计模式学习--Singleton
- 设计模式C++学习笔记之三(Singleton单例模式)
- 关于最近公司开展内训讲课实例代码等架构设计方面的案例说明
- java语言讲解singleton的编程思想---深入浅出单实例Singleton设计模式
- [导入]关于整理出的设计模式系列各方面学习资料
- 设计模式实例与代码:Factory Method模式
- 跟着实例学习设计模式(8)-适配器模式adapter(结构型)
- 步步为营 .NET 设计模式学习笔记 四、Singleton(单例模式)
- 绝对精辟,鲜活代码和实例讲授门面设计模式
- 坏狼php学习 计数器实例代码
- 深入浅出单实例Singleton设计模式