这个机制原理

解决方案 »

  1.   

    System.Threading.Monitor.Enter(locker);
    System.Threading.Monitor.Exit(locker);的简写罢了,看msdn关于monitor的解释。
      

  2.   

    说简写不是很正确,应该说lock是用System.Threading.Monitor实现的。下面内容摘自下面文章In fact, the lock keyword is implemented with the Monitor classUsing the lock keyword is generally preferred over using the Monitor class directly, both because lock is more concise, and because lock insures that the underlying monitor is released, even if the protected code throws an exception.参考
    Thread Synchronization (C# Programming Guide)
      

  3.   


    推荐一篇文章,很好的解释lock(object).
    C#深入学习笔记---Lock,希望对你在帮助
      

  4.   

    锁住当前实例:lock(this)  
    锁住此类的所有实例:lock(typeof([Type]))  
    对字符串的锁,会锁定所有相同内容的字符串,建议可以用静态字符串代替  
    lock关键字比Monitor简洁,其实lock就是对Monitor的Enter和Exit的一个封装  
    lock是一种比较好用的简单的线程同步方式
    public void Function()
    {
    object lockThis = new object();
    lock (lockThis)
    {
    }
    }
    还可使用monitor,mutex,ReaderWriterLock 
    lock