/// <summary> /// 静态字段在程序进程只有一个 /// </summary> //public static Singleton singleton = new Singleton(); static void Main(string[] args) { try { SingletonSecond second = SingletonSecond.CreateInstance(); SingletonThird third = SingletonThird.CreateInstance(); //Singleton singleton = new Singleton(); //对象的重用 TaskFactory taskFactory = new TaskFactory(); List <Task> taskList = new List <Task>(); //for (int i = 0; i < 50000; i++) //{ // taskList.Add(taskFactory.StartNew(() => // { // Singleton singleton = Singleton.CreateInstance();// new Singleton(); // singleton.Show();//多线程去运行同一个实例的同一个方法 // })); //} for (int i = 0; i < 50000; i++) { taskList.Add(taskFactory.StartNew(() => { SingletonSecond singleton = SingletonSecond.CreateInstancePrototype();// new Singleton(); singleton.Show(); })); } Task.WaitAll(taskList.ToArray()); Console.WriteLine("第一轮全部完成"); //for (int i = 0; i < 5; i++) //{ // taskList.Add(taskFactory.StartNew(() => // { // Singleton singleton = Singleton.CreateInstance();// new Singleton(); // //singleton.Show(); // })); //} //0 1 50000 还是接近50000 Console.WriteLine(SingletonSecond.CreateInstancePrototype().iTotal); //for (int i = 0; i < 5; i++) //{ // taskFactory.StartNew(() => // { // Singleton singleton = Singleton.CreateInstance();// new Singleton(); // singleton.Show(); // }); //} //OtherClass.Show(); //OtherClass.Show(); //OtherClass.Show(); } catch (Exception ex) { Console.WriteLine(ex.Message); } Console.Read(); }