One advantage of using multiple threads in an application is that each thread can execute asynchronously. For Windows applications, time-consuming tasks can be performed in the background, allowing application windows and controls to remain responsive. For server applications, multithreading provides the ability to process each incoming request with a different thread. Otherwise, each new request cannot be processed until the previous request is fully satisfied.
However, the asynchronous nature of threads means that access to resources, such as file handles, network connections, and memory, must be coordinated. Otherwise, two or more threads may access the same resources at the same time, and each thread does not know what the other threads are doing. The results will produce unpredictable data corruption.
Let's take a look at a Java example: multithreading may cause problems and their solutions
The following is the code for C #
Transfer method
public void transfer (int from, int to, double amount) {
if (Accounts[from] < amount) return;
Console.WriteLine (System.Threading.Thread.CurrentThread.Name);
Accounts[from]-= amount;
Console.WriteLine (' {0:f2} from {1} ' to {2} ', amount, from, to);
Accounts[to] + = amount;
Console.WriteLine ("Total Balance: {0:f2}", Gettotalbalance ());
}run method
public void Run () {
Random Rand;
try {
while (true) {
Rand = new Random ();
int toaccount = rand. Next (bank.size);
Double amount = rand. Nextdouble () * MAXAMOUNT;
Bank.transfer (Fromaccount, Toaccount, amount);
System.Threading.Thread.Sleep (Rand. Next (dely));
}
}
Catch {}
}
This problem is also present in. NET, and for. NET there are several solutions to ensure the safe execution of multithreading:
1) lock keyword
2) Monitor
3 synchronization event and wait handle
4) Mutex Object
Let's just say here. Use the lock keyword, and if you need more information, visit MSDN:
Ms-help://ms. Msdnqtr.v80.chs/ms. Msdn.v80/ms. Visualstudio.v80.chs/dv_csref/html/413e1f28-a2c5-4eec-8338-aa43e7982ff4.htm
The lock keyword can be used to ensure that a block of code finishes running without being interrupted by another thread. This is accomplished by acquiring a mutex for a given object while the code block is running.
The lock statement begins with the keyword lock, which has an object as a parameter, followed by a block of code that can only be executed by one thread at a time. For example:
public void Function ()
{
System.Object lockThis = new System.Object ();
Lock (LockThis)
{
Access thread-sensitive Resources.
}
}
Modify the Run method
public void Run () {
Random Rand;
try{
while (true) {
Lock (Bank) {
Rand = new Random ();
int toaccount = rand. Next (bank.size);
Double amount = rand. Nextdouble () * MAXAMOUNT;
Bank.transfer (Fromaccount, Toaccount, amount);
System.Threading.Thread.Sleep (Rand. Next (dely));
}
}
}
catch{}
Lock the instance of the Bank object with the Lock keyword, lock ensures that when one thread is in the critical section of the code, the other thread does not enter the critical section. If another thread tries to enter the locked code, it waits (is blocked) until the object is released.
The use of lock here requires attention:
You should avoid locking the public type, otherwise the instance will go beyond the control of your code. Common structure Lock (this), Lock (typeof (MyType)), and Lock ("MyLock") violate this guideline:
1. If the instance can be publicly accessed, the lock (this) problem occurs.
2. Lock (typeof (MyType) problem occurs if MyType can be accessed publicly
3. Lock ("MyLock") problem occurs because any other code in the process that uses the same string will share the same lock.
Personally I think multithreading if you want to access shared data must be synchronized