title: C# Multithreading with Lock
body: <p>I am a beginner to C#. I am taking a look at sample of multithreading.</p>
  
  <p>I have set up a multithreaded call to method <code>DoTransactions()</code>, which in turn will generate a random number as <code>amount</code>, and call method <code>Withdraw()</code> to deduct <code>salary</code> by <code>amount</code>.</p>
  
  <p>1) What I don't understand is that why the writer chose to lock Object <code>thisLock</code> instead of locking salary? I saw some others also lock thread in this way by declaring an object and locking it.</p>
  
  <p>2) Sometimes I cannot debug the methods called when the other threads were running. (by pressing F10/F11). (e.g. methods <code>Withdraw()</code>). Any reason for this?</p>
  
  <pre><code>Department dep = new Department(1000); 
  Thread t = new Thread(new ThreadStart(dep.DoTransactions));   //set up 1000 threads.
  
  class Department
      {
          private Object thisLock = new Object();
          int salary = 10000;
  
          int Withdraw(int amount)
          {
              lock (thisLock)
              {
                  if (salary &gt;= amount)
                  {
                      salary = salary - amount;
                      return amount;
                  }
              }
          }
  </code></pre>