lock關鍵字可以用來確保代碼塊完成運行,而不會被其他線程中斷。這是通過在代碼塊運行期間為給定對象擷取互斥鎖來實現的。
首先定義Account類,如下所示:
public class Account{private int balance = 500;public void WithDraw(int amount){if (balance - amount > 0){Console.WriteLine("{1}:before drawing:balance:{0},amount:{2}", balance, Thread.CurrentThread.Name, amount);Thread.Sleep(500);balance -= amount;Console.WriteLine("{1}:after drawing:balance:{0},amount:{2}", balance, Thread.CurrentThread.Name, amount);Console.WriteLine();}else{Console.WriteLine("{0}:no money", Thread.CurrentThread.Name);Thread.CurrentThread.Abort();}}public void DoTrans(){Random ran = new Random();for (int i = 0; i < 10; i++){WithDraw(ran.Next(50, 100));}}}
該類中定義了某個帳戶初使金額是500元,WithDraw方法用於從帳戶中取錢,並在取錢之前判斷要取的金額是否大於餘額,在判斷後如果滿足條件則執行減操作,此處使用Thread.Sleep方法類比操作過程中一些耗時操作以允許其他線程有機會執行.
DoTrans方法用於分10次隨機從帳戶中取款.接下來定義Main方法使用兩個線程取款:
static void Main(string[] args){Account a1 = new Account();Thread t1 = new Thread(new ThreadStart(a1.DoTrans));Thread t2 = new Thread(new ThreadStart(a1.DoTrans));t1.Name = "Thread 11111";t2.Name = "Thread 22222";t1.Start();t2.Start();Console.ReadLine();}
運行項目會發現由於兩個線程同時取款,在其中一個線程判斷滿足條件後休眠,由另外一個線程取款,取完款後再返回來執行第一個線程的取款操作,此時一線程已經執行過判斷,繼承取款會導致負數餘額的產生.
因此解決方案可以使用lock關鍵字,將Account的WithDraw方法修正如下:
public void WithDraw(int amount){lock (this){if (balance - amount > 0){Console.WriteLine("{1}:before drawing:balance:{0},amount:{2}", balance, Thread.CurrentThread.Name, amount);Thread.Sleep(500);balance -= amount;Console.WriteLine("{1}:after drawing:balance:{0},amount:{2}", balance, Thread.CurrentThread.Name, amount);Console.WriteLine();}else{Console.WriteLine("{0}:no money", Thread.CurrentThread.Name);Thread.CurrentThread.Abort();}}}
可以保證該代碼塊不能被中斷,從而使帳戶金額不會產生負數的情況