This article introduces the C # lock keyword. C # provides a keyword lock, which defines a piece of code as a mutex section ), A mutex can only be executed by one thread at a time, while other threads must wait.
Each thread has its own resources, but the code zone is shared, that is, each thread can execute the same function. This may cause several threads to execute a function at the same time, resulting in data confusion and unexpected results. Therefore, we must avoid this situation. 
C # provides a keyword lock, which defines a piece of code as a critical section. A mutex section allows only one thread to enter the execution at a time point, other threads must wait. The C # lock keyword is defined as follows: 
 
Lock (expression) statement_block
Expression indicates the object you want to trace. It is usually an object reference. 
If you want to protect an instance of a class, generally you can use this; if you want to protect a static variable (such as a mutex code segment inside a static method ), generally, you can use a class name. 
The statement_block is the code of the mutex, which can be executed by only one thread at a time. 
The following is a typical example of using the C # lock keyword. The usage and usage of the C # lock keyword are described in the annotations. 
 
Example:
 
Copy codeThe Code is as follows: using System; 
Using System. Threading; 
Namespace ThreadSimple 
{ 
Internal class Account 
{ 
Int balance; // balance 
Random r = new Random (); 
Internal Account (int initial) 
{ 
Balance = initial; 
} 
Internal int Withdraw (int amount) // retrieves and withdraws money 
{ 
If (balance <0) 
{ 
// If the balance is smaller than 0, an exception is thrown. 
Throw new Exception ("NegativeBalance"); // negative balance 
} 
// The following Code ensures that the balance value is modified by the current thread. 
// No other threads will execute this code to modify the balance value. 
// Therefore, the balance value cannot be smaller than 0. 
Lock (this) 
{ 
Console. WriteLine ("CurrentThread:" + Thread. CurrentThread. Name ); 
// If the lock keyword is not protected, it may be after the if condition judgment (true) is executed. 
// Another thread executes the balance = balance-amount command to modify the balance value. 
// This modification is invisible to this thread, so it may cause the if condition to be invalid. 
// However, this thread continues to execute balance = balance-amount, so the balance may be less than 0 
If (balance> = amount) 
{ 
Thread. Sleep (5 ); 
Balance = balance-amount; 
Return amount; 
} Else 
{ 
Return 0; 
// Transactionrejected 
} 
} 
} 
Internal void DoTransactions () // withdrawal transaction 
{ 
For (int I = 0; I <100; I ++) 
{ 
Withdraw (r. Next (-50,100 )); 
} 
} 
} 
Internal class Test 
{ 
Static internal Thread [] threads = new Thread [10]; 
Public static void Main () 
{ 
Account acc = new Account (0 ); 
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]. Name = I. ToString (); 
} 
For (int I = 0; I <10; I ++) 
{ 
Threads [I]. Start (); 
Console. ReadLine (); 
} 
} 
} 
}