singleton及多线程验证,所有线程完成才继续运行WaitHandle

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace SingletonDemo
{

    public sealed class Singleton
    {
        private Singleton() { }

        private static Singleton ins = null;
        private static object obj = new object();

        public static Singleton Instance
        {
            get
            {
                if (ins == null)
                {
                    System.Threading.Thread.Sleep(1000);
               
                    lock (obj)
                    {
                        if (ins == null)
                        {
                            ins = new Singleton();
                        }
                    }
                }
                return ins;
            }
        }

        public void SaySth()
        {
            Console.WriteLine("hello");
        }
        public int MyProperty { get; set; }
    }


    class Program
    {
        static Singleton sin1 = null, sin2 = null;

        static bool b = false;

        static void GetInstance( object obj)
        {            
            if (b==false)
            {
                b = true;
                sin1 = Singleton.Instance;                
            }
            else 
            {
                sin2 = Singleton.Instance;
            }
            (obj as ManualResetEvent).Set();
        }

        static void Main(string[] args)
        {
            //Singleton s1 = new Singleton();
            //Singleton s2 = new Singleton();

            //Singleton ins = Singleton.Instance;
            //ins.SaySth();
            //ins.MyProperty = 13;
            //Console.WriteLine(ins.MyProperty);


            //Console.WriteLine("--------------------");

            //Singleton ins2 = Singleton.Instance;
            //ins2.SaySth();
            //Console.WriteLine(ins2.MyProperty);

            //bool b = Object.ReferenceEquals(ins, ins2);
            //Console.WriteLine(b);

            ManualResetEvent[] manualEvents = new ManualResetEvent[2];

            manualEvents[0] = new ManualResetEvent(false);
            manualEvents[1] = new ManualResetEvent(false);

            Thread thread1 = new Thread(
                new ParameterizedThreadStart(GetInstance));

            Thread thread2 = new Thread(
             new ParameterizedThreadStart(GetInstance));

            thread1.Start(manualEvents[0]);
            thread2.Start(manualEvents[1]);

            WaitHandle.WaitAll(manualEvents);

            //ThreadPool.QueueUserWorkItem(GetInstance, manualEvents[0]);
            //ThreadPool.QueueUserWorkItem(GetInstance, manualEvents[1]); 
            
            sin1.SaySth();
            sin2.SaySth();

            sin1.MyProperty = 1;
            sin2.MyProperty = 2;

            bool b = Object.ReferenceEquals(sin1, sin2);
            Console.WriteLine(b);

            Console.WriteLine("sin1:" + sin1.MyProperty);
            Console.WriteLine("sin2:" + sin2.MyProperty);

            Console.ReadKey();
        }

    }
}

posted @ 2010-10-28 00:42  庚武  Views(306)  Comments(0Edit  收藏  举报