Пример #1
0
        static void Main(string[] args)
        {
            try
            {
                //{//单线程情况下
                //    for (int i = 0; i < 10; i++)
                //    {
                //        Singleton singleton = Singleton.CreateInstance();
                //        Console.WriteLine(singleton.GetResult());
                //    }
                //}
                //{//多线程情况下
                //    List<Task> tasks = new List<Task>();
                //    TaskFactory taskFactory = new TaskFactory();
                //    for (int i = 0; i < 10; i++)
                //    {
                //        tasks.Add(taskFactory.StartNew(() =>
                //        {
                //            Singleton singleton = Singleton.CreateInstance();
                //            Console.WriteLine(singleton.GetResult());
                //        }));
                //    }
                //    Task.WaitAll(tasks.ToArray());
                //}

                //{//多线程情况下(静态构造函数)
                //    List<Task> tasks = new List<Task>();
                //    TaskFactory taskFactory = new TaskFactory();
                //    for (int i = 0; i < 10; i++)
                //    {
                //        tasks.Add(taskFactory.StartNew(() =>
                //        {
                //            SingletonSecond singleton = SingletonSecond.CreateInstance();
                //            Console.WriteLine(singleton.GetResult());
                //        }));
                //    }
                //    Task.WaitAll(tasks.ToArray());
                //}
                {//多线程情况下(静态对象)
                    List <Task> tasks       = new List <Task>();
                    TaskFactory taskFactory = new TaskFactory();
                    for (int i = 0; i < 10; i++)
                    {
                        tasks.Add(taskFactory.StartNew(() =>
                        {
                            SingletonThird singleton = SingletonThird.CreateInstance();
                            Console.WriteLine(singleton.GetResult());
                        }));
                    }
                    Task.WaitAll(tasks.ToArray());
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("" + ex.Message);
            }
            Console.ReadKey();
        }
Пример #2
0
        static void Main(string[] args)
        {
            try
            {
                {
                    //for (int i = 0; i < 5; i++)
                    //{
                    //    Singleton singleton = Singleton.CreateInstance();
                    //    singleton.WriteText();
                    //}
                }
                {
                    //TaskFactory taskFactory = new TaskFactory();
                    //for (int i = 0; i < 5; i++)
                    //{
                    //    taskFactory.StartNew(() =>
                    //    {
                    //        Singleton singleton = Singleton.CreateInstance();
                    //        singleton.WriteText();
                    //    });
                    //}
                }
                {
                    //for (int i = 0; i < 5; i++)
                    //{
                    //    SingletonSecond singleton = SingletonSecond.CreateInstance();
                    //    singleton.WriteText();
                    //}

                    TaskFactory taskFactory = new TaskFactory();
                    for (int i = 0; i < 5; i++)
                    {
                        taskFactory.StartNew(() =>
                        {
                            SingletonThird singleton = SingletonThird.CreateInstance();
                            singleton.WriteText();
                        });
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            Console.Read();
        }
Пример #3
0
        static void Main(string[] args)
        {
            try
            {
                List <IAsyncResult> resultList = new List <IAsyncResult>();
                for (int i = 0; i < 10; i++)
                {
                    resultList.Add(new Action(() =>
                    {
                        Singleton singleton = Singleton.CreateInstance();
                        singleton.Show();
                    }).BeginInvoke(null, null));//会启动一个异步多线程调用
                }

                while (resultList.Count(r => !r.IsCompleted) > 0)
                {
                    Thread.Sleep(10);
                }


                for (int i = 0; i < 10; i++)
                {
                    resultList.Add(new Action(() =>
                    {
                        //SingletonSecond singleton = SingletonSecond.CreateInstance();
                        SingletonThird singleton = SingletonThird.CreateInstance();
                        singleton.Show();
                    }).BeginInvoke(null, null));//会启动一个异步多线程调用
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            Console.Read();
        }
Пример #4
0
        /// <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();
        }