int Withdraw(int amount)
{
if(balance < 0)
{
throw new Exception("not enough balance");
}
Monitor.Enter(aLock); // this is where the lock begins
try
{
if(this.balance >= amount)
{
Console.WriteLine("Amount drawn: " + amount);
this.balance -= amount;
return this.balance;
}
}
finally
{
Monitor.Exit(aLock); // finish the lock
}
return 0;
}