Exemplo n.º 1
0
        public static void UseSingleton()
        {
            Console.WriteLine("This is StudentC");
            Singleton singleton = Singleton.CreateInstance();// new Singleton();

            singleton.Show();
        }
Exemplo n.º 2
0
        //public static Singleton singletonCommon = new Singleton();//只构造一个对象

        static void Main(string[] args)
        {
            try
            {
                Console.WriteLine("欢迎来到.net高级班公开课之设计模式特训,今天是Eleven老师为大家带来的单例模式Singleton");

                ////Singleton singleton = Singleton.CreateInstance(); //new Singleton();//只构造一个对象
                ////for (int i = 0; i < 10; i++)
                ////{
                ////    Singleton singleton = Singleton.CreateInstance();
                ////    Singleton singleton = new Singleton();//每次都构造一个对象
                ////    singletonCommon.Show();
                ////    singleton.Show();
                ////}

                ////StudentA.UseSingleton();
                ////StudentB.UseSingleton();
                ////StudentC.UseSingleton();

                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();
                        singleton.Show();
                    }).BeginInvoke(null, null));//会启动一个异步多线程调用
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            Console.Read();
        }
Exemplo n.º 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();
        }