找了不少资料,都说lock后对象就被锁锁定,只有到退出lock后对象才能被访问到(有可能是表达或翻译问题)
而下面这段代码表现出的特点不符合上面的说法
  
   class StateObject
    {
        public  int Data = 0;
        
        public void Run()
        {
          
                for (int i = 0; i < 1000; i++)
                {
                    Add(1);
                   
                }
           
        }
        public void Add(int num)
        {
            lock (this)
            {
                Data = 0;
                Data += num;
                Thread.Sleep(1);
                Data -= num;
                Trace.Assert(Data == 0, "出现不一致的情况 在:" + Data);
                Console.WriteLine("Thread:" + Thread.CurrentThread.Name + "  Data:=" + Data);
                
            }
            
        }
        public void Run2()
        {
            for (int i = 0; i < 1000; i++)
            {
                Add2(1);
            }
        }
        public void Add2(int num)
        {
           
                Data += num;
                Thread.Sleep(1);
                Data -= num;
                Console.WriteLine("Thread:" + Thread.CurrentThread.Name + "  Data:=" + Data);
                        }
  
    }
    class Program
    {
       
        static void Main(string[] args)  
        {
            Console.ReadLine();
            StateObject s = new StateObject();
            StateObject s2 = new StateObject();
            StateObject s3 = new StateObject();
            Thread t1 = new Thread(new ThreadStart(s.Run));
            Thread t2 = new Thread(new ThreadStart(s.Run2));
  
            t1.Name = "#1"; t2.Name = "#2";            t1.Start();
            t2.Start();
            t1.Join();
            t2.Join();            Console.ReadLine();
        }    }
以上代码允许后断言会失败
说明lock后,其他线程(未lock同一个对象的代码段如Add2)可以访问用于lock的对象,
----------------------