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 Itext Pdf】itext pdf隔行换色 itext5添加表格背景颜色
    linux常用安装
    linux 定时执行shell脚本
    Linux crontab定时任务案例
    Oracle 常用的几个命令
    RS报表中根据变量比较大小来判断颜色
    如何利用TYPE 实现列转行
    Linux crontab概念
    关于下拉框联动选择的做法
    Oracle 启动常见的疑难
  • 原文地址:https://www.cnblogs.com/wucg/p/1863072.html
Copyright © 2011-2022 走看看