关于lock的对象的问题
我目前的理解,lock()内的对象就是在这个对象上设置了一个值。   
 我的问题是,难道加了lock就自动为这个对象创造这个属性么,它可以单独得到么。编译器依靠什么来使之具有安全性。   
 另一个问题是,如果放进去的是一个值对象,那会怎么样呢 
------解决方案--------------------lock 确保当一个线程位于代码的临界区时,另一个线程不进入临界区。如果其他线程试图进入锁定的代码,则它将一直等待(即被阻止),直到该对象被释放。   
 因此他只是将语句块标记为临界区,方法是获取给定对象的互斥锁,执行语句,然后释放该锁。   
 下例使用线程和 lock。只要 lock 语句存在,语句块就是临界区并且 balance 永远不会是负数。   
 // statements_lock2.cs 
 using System; 
 using System.Threading;   
 class Account 
 { 
     private Object thisLock = new Object(); 
     int balance;   
     Random r = new Random();   
     public Account(int initial) 
     { 
         balance = initial; 
     }   
     int Withdraw(int amount) 
     {   
         // This condition will never be true unless the lock statement 
         // is commented out: 
         if (balance  < 0) 
         { 
             throw new Exception( "Negative Balance "); 
         }   
         // Comment out the next line to see the effect of leaving out  
         // the lock keyword: 
         lock(thisLock) 
         { 
             if (balance > = amount) 
             { 
                 Console.WriteLine( "Balance before Withdrawal :   " + balance); 
                 Console.WriteLine( "Amount to Withdraw        : - " + amount); 
                 balance = balance - amount; 
                 Console.WriteLine( "Balance after Withdrawal  :   " + balance); 
                 return amount; 
             } 
             else 
             { 
                 return 0; // transaction rejected 
             } 
         } 
     }   
     public void DoTransactions() 
     { 
         for (int i = 0; i  < 100; i++) 
         { 
             Withdraw(r.Next(1, 100)); 
         } 
     } 
 }   
 class Test 
 { 
     static void Main() 
     { 
         Thread[] threads = new Thread[10]; 
         Account acc = new Account(1000); 
         for (int i = 0; i  < 10; i++) 
         { 
             Thread t = new Thread(new ThreadStart(acc.DoTransactions)); 
             threads[i] = t; 
         } 
         for (int i = 0; i  < 10; i++) 
         { 
             threads[i].Start(); 
         } 
     } 
 }    
------解决方案--------------------lock主要和线程结合在一起 lz要搞清楚lock使用的最初目的
------解决方案--------------------我目前的理解,lock()内的对象就是在这个对象上设置了一个值。 
 ___________________________________________________________   
 这个不太确切. 在.NET里Object的Lock同步机制是这样的:   
 每一个System.Object 产生实例的时候,它内部都有一个特殊的SyncBlockIndex , 而lock 的实质只是从内存的 SyncBlocks 表中取出一个可用的,并把它的Index 存到 Object 的 SyncBlockIndex 中.   
 详细解释请看大师Jeffrey Ritcher 写的这篇文章,把.NET的同步机制交代的十分彻底:   
 http://msdn.microsoft.com/msdnmag/issues/03/01/NET/