static void Main(string[] args) { Console.Write("欢迎学习单例模式"); Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); /*单线程*/ //for(int i=0;i<10;i++) //{ // Singleton singleton = new Singleton();//实例化 // singleton.SayHi(); // Console.WriteLine("最终需要的是{0}", singleton.GetResult()); //} List <Task> taskList = new List <Task>(); TaskFactory taskFactory = new TaskFactory(); for (int i = 0; i < 10; i++) { /*启用多线程 都随机的进入,占用CPU资源.*/ taskList.Add(taskFactory.StartNew(() => { //Singleton singleton = new Singleton();//写到这里的话,就会启用很多对象,在构造函数时候私有化,类给一个方法出来 SingletonSecond singleton = SingletonSecond.CreateInstance(); singleton.SayHi(); Console.WriteLine("最终需要的是{0},ThreadId={1}", singleton.GetResult(), Thread.CurrentThread.ManagedThreadId); })); } Task.WaitAll(taskList.ToArray()); stopwatch.Stop(); Console.WriteLine("一共耗时{0}毫秒", stopwatch.ElapsedMilliseconds); for (int i = 0; i < 10; i++) { /*启用多线程 都随机的进入,占用CPU资源.*/ taskList.Add(taskFactory.StartNew(() => { //Singleton singleton = new Singleton();//写到这里的话,就会启用很多对象,在构造函数时候私有化,类给一个方法出来 SingletonSecond singleton = SingletonSecond.CreateInstance(); singleton.SayHi(); Console.WriteLine("最终需要的是{0},ThreadId={1}", singleton.GetResult(), Thread.CurrentThread.ManagedThreadId); })); } Console.ReadKey(); }
static SingletonSecond()//CLR运行时候 第一次使用这个类之前,一定会而且只会执行一次 { _SingletonSecond = new SingletonSecond(); }