public static void TestLockStatements() { var account = new BankAcc(1000); var tasks = new Task[2]; for (int i = 0; i < tasks.Length; i++) { tasks[i] = Task.Run(() => UpdateAccount(account)); } Task.WaitAll(tasks); }
private static void UpdateAccount(BankAcc account) { var rnd = new Random(); for (int i = 0; i < 10; i++) { var amount = rnd.Next(1, 1000); bool doCredit = rnd.NextDouble() < 0.5; if (doCredit) { account.Credit(amount); } else { account.Debit(amount); } } }