扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
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; } |
如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。
现场直击|2021世界人工智能大会
直击5G创新地带,就在2021MWC上海
5G已至 转型当时——服务提供商如何把握转型的绝佳时机
寻找自己的Flag
华为开发者大会2020(Cloud)- 科技行者