zoukankan      html  css  js  c++  java
  • 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();
            }
    
        }
    }
    
    
  • 相关阅读:
    C语言——总结回顾
    C语言——第十四、十五周作业
    题目思路——统计素数并求和
    题目思路——单词长度
    C语言——第七周作业
    C语言——第六周作业
    C语言——第四次作业
    C语言——第三次作业
    C语言——第二次作业
    C语言——第一次作业
  • 原文地址:https://www.cnblogs.com/wucg/p/1863072.html
Copyright © 2011-2022 走看看