net 多线程 学习 05 Monitor 锁


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;
}


作者:spike

分类: Net

创作时间:2023-06-25

更新时间:2024-12-09

联系方式放在中括号之中例如[[email protected]],回复评论在开头加上标号例如:#1