混合锁、互斥锁

    public static class Sample2
    {
        //互斥锁和自旋锁不同的是  互斥锁获取锁失败后,会等待,而不是重新尝试获取.因为等待所以不会消耗资源。
        //线程从等待到唤醒状态到调度运行需要花费一定的时间
        // Mutex  互斥锁
        private static readonly Mutex mutex = new Mutex();
        public static void test()
        {
          //  mutex.WaitOne(1000);
           // mutex.ReleaseMutex();
        }
        //混合锁  适合大部分场景
        // Moniter  等同于  lock
        private  static  object locker =new  object();
        public static void test1()
        {
            
            Monitor.Enter(locker);

            Monitor.Exit(locker);
        }


    }

  

posted @ 2020-07-31 14:00  谁说程序猿很猥琐  阅读(225)  评论(0编辑  收藏  举报