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();
            }
    
        }
    }
    
    
  • 相关阅读:
    计算机网络的三种通讯模式(单播,广播,组播)
    java字符串面试题
    java使用纯命令行打包项目
    java字节码的工具(含IDEA插件)
    Spring配置之context:annotation与、component-scan以及annotation-driven
    Java ThreadLocal的使用案例
    对称平方数(to_string函数,stoi函数真香)
    字符串最后一位长度
    缺失的括号
    数三角形
  • 原文地址:https://www.cnblogs.com/wucg/p/1863072.html
Copyright © 2011-2022 走看看