static void Main(string[] args)
        {
            string sql = "select id from tb_area";
            object obj = SqlHelper.ExecuteScalar(sql);

            Console.WriteLine(obj);



            // 不能创建实例 应为默认的构造函数被私有化了
            // SingletonClass1 sgt1 = new SingletonClass1();

            //不管你调用几次,都只能创建一个对象

            /* SingletonClass1 sgt1 = SingletonClass1.CreateInstance();
             * SingletonClass1 sgt2 = SingletonClass1.CreateInstance();
             * SingletonClass1 sgt3 = SingletonClass1.CreateInstance();*/

            /*  //使用循环创建多个线程来测试
             * for (int i = 0; i < 1000; i++)
             * {
             *    //创建一个线程
             *    Thread t1 = new Thread(new ThreadStart(()=> {
             *        SingletonClass1 sgt = SingletonClass1.CreateInstance();
             *    }));
             *    t1.Start();
             * }*/

            for (int i = 0; i < 10; i++)
            {
                //创建线程
                Thread t2 = new Thread(new ThreadStart(() =>
                {
                    SingletonClass2 sgt2 = SingletonClass2.CreateInstance();
                    sgt2.SayHi();
                }));
                t2.Start();//启动线程
            }
            string txt = "123";
            //调用自定义的类库
            string cipher = TekinFLib.Security.MyHash.GetHash(txt);

            Console.WriteLine($" {txt}加密后的字符串为{cipher}");

            Console.WriteLine("OK");

            Console.ReadKey();
        }
示例#2
0
 private static readonly Object syn = new object();//定义一个对象用于加锁
 public static SingletonClass2 CreateInstance()
 {
     //注意这里的双重if判断 来确保只能创建一次 且性能最优化
     if (null == _instance)
     {
         lock (syn)
         {
             //在锁住后再判断是否需要创建,这样就确保只能创建一次
             if (_instance == null)
             {
                 _instance = new SingletonClass2();
             }
         }
     }
     return(_instance);
 }