C#中lock關鍵字

來源:互聯網
上載者:User

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

可以保證該代碼塊不能被中斷,從而使帳戶金額不會產生負數的情況

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.