此文章由人工翻譯。 將光標移到文章的句子上,以查看原文。 更多信息。
譯文
原文
“鎖定”語句(C# 參考)

<?XML:NAMESPACE PREFIX = "[default] http://www.w3.org/1999/xhtml" NS = "http://www.w3.org/1999/xhtml" />lock 關鍵字將語句塊標記為臨界區,方法是獲取給定對象的互斥鎖,執行語句,然后釋放該鎖。 下面的示例包含一個lock 語句。
class Account
{
decimal balance;
private Object thisLock = new Object();
public void Withdraw(decimal amount)
{
lock (thisLock)
{
if (amount > balance)
{
throw new Exception("Insufficient funds");
}
balance -= amount;
}
}
}
有關更多信息,請參見 線程同步(C# 和 Visual Basic)。
lock 關鍵字可確保當一個線程位於代碼的臨界區時,另一個線程不會進入該臨界區。 如果其他線程嘗試進入鎖定的代碼,則它將一直等待(即被阻止),直到該對象被釋放。
線程處理(C# 和 Visual Basic) 這節討論了線程處理。
lock 關鍵字在塊的開始處調用 Enter,而在塊的結尾處調用 Exit。 ThreadInterruptedException 引發,如果Interrupt 中斷等待輸入 lock 語句的線程。
通常,應避免鎖定 public 類型,否則實例將超出代碼的控制范圍。 常見的結構 lock (this)、lock (typeof (MyType))和 lock ("myLock") 違反此准則:
-
如果實例可以被公共訪問,將出現 lock (this) 問題。
-
如果 MyType 可以被公共訪問,將出現 lock (typeof (MyType)) 問題。
-
由於進程中使用同一字符串的任何其他代碼都將共享同一個鎖,所以出現 lock("myLock") 問題。
最佳做法是定義 private 對象來鎖定, 或 private static 對象變量來保護所有實例所共有的數據。
在 lock 語句的正文不能使用 等待 關鍵字。
下面演示在 C# 中使用未鎖定的線程的簡單示例。
//using System.Threading;
class ThreadTest
{
public void RunMe()
{
Console.WriteLine("RunMe called");
}
static void Main()
{
ThreadTest b = new ThreadTest();
Thread t = new Thread(b.RunMe);
t.Start();
}
}
// Output: RunMe called
下例使用線程和 lock。 只要 lock 語句存在,語句塊就是臨界區並且 balance 永遠不會是負數。
// using System.Threading;
class Account
{
private Object thisLock = new Object();
int balance;
Random r = new Random();
public Account(int initial)
{
balance = initial;
}
int Withdraw(int amount)
{
// This condition never is true unless the lock statement
// is commented out.
if (balance < 0)
{
throw new Exception("Negative Balance");
}
// Comment out the next line to see the effect of leaving out
// the lock keyword.
lock (thisLock)
{
if (balance >= amount)
{
Console.WriteLine("Balance before Withdrawal : " + balance);
Console.WriteLine("Amount to Withdraw : -" + amount);
balance = balance - amount;
Console.WriteLine("Balance after Withdrawal : " + balance);
return amount;
}
else
{
return 0; // transaction rejected
}
}
}
public void DoTransactions()
{
for (int i = 0; i < 100; i++)
{
Withdraw(r.Next(1, 100));
}
}
}
class Test
{
static void Main()
{
Thread[] threads = new Thread[10];
Account acc = new Account(1000);
for (int i = 0; i < 10; i++)
{
Thread t = new Thread(new ThreadStart(acc.DoTransactions));
threads[i] = t;
}
for (int i = 0; i < 10; i++)
{
threads[i].Start();
}
}
}
有關詳細信息,請參閱 C# 語言規范。該語言規范是 C# 語法和用法的權威資料。
