using System; namespace csPattern.Singleton { public class Singleton { static Singleton uniSingleton = new Singleton(); private Singleton() {} static public Singleton instance() { return uniSingleton; } } } |
using System; namespace csPattern.Singleton { public class Singleton { static Singleton uniSingleton; private Singleton() {} static public Singleton instance() { if (null == uniSingleton) { uniSingleton = new Singleton _lazy(); } return uniSingleton; } } } |
using System; using System.Threading; namespace csPattern.Singleton { public class Counter { static Counter uniCounter = new Counter(); //存储唯一的实例。 private int totNum = 0; //存储计数值。 private Counter() { Thread.Sleep(100); //这里假设因为某种因素而耽搁了100毫秒。 //在非lazy initialization 的情况下, 不会影响到计数。. } static public Counter instance() { return uniCounter; } public void Inc() { totNum ++;} //计数加1。 public int GetCounter() { return totNum;} //获得当前计数值。 } } |
using System; using System.IO; using System.Threading; namespace csPattern.Singleton.MutileThread { public class MutileClient { public MutileClient() {} public void DoSomeWork() { Counter myCounter = Counter.instance(); //方法一 //Counter_lazy myCounter = Counter_lazy.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(); DoSomeWork(); //线程0也只执行和其他线程相同的工作。 } } } |
using System; namespace csPattern.Singleton { public class RunMain { public RunMain() {} static public void Main(string[] args) { MutileThread.MutileClient myClient = new MutileThread.MutileClient(); myClient.ClientMain(); System.Console.ReadLine(); } } } |
using System; using System.Threading; using System.Runtime.CompilerServices; namespace csPattern.Singleton { public class Counter_lazy { static Counter_lazy uniCounter; private int totNum = 0; private Counter_lazy() { Thread.Sleep(100); //假设多线程的时候因某种原因阻塞100毫秒 } [MethodImpl(MethodImplOptions.Synchronized)] //方法的同步属性 static public Counter_lazy instance() { if (null == uniCounter) { uniCounter = new Counter_lazy(); } return uniCounter; } public void Inc() { totNum ++;} public int GetCounter() { return totNum;} } } |
using System; using System.Threading; namespace csPattern.Singleton { public class Counter_lazy { static Counter_lazy uniCounter; static object myObject = new object(); private int totNum = 0; private Counter_lazy() { Thread.Sleep(100); //假设多线程的时候因某种原因阻塞100毫秒 } static public Counter_lazy instance() { lock(myObject) { if (null == uniCounter) { uniCounter = new Counter_lazy(); } return uniCounter; } } public void Inc() { totNum ++;} public int GetCounter() { return totNum;} } } |
static public Counter_lazy instance() { mut.WaitOne(); if (null == uniCounter) { uniCounter = new Counter_lazy(); } mut.ReleaseMutex(); return uniCounter; } |