At the beginning of programming, there is a blind worship of multithreading. When you encounter a method that calls write, you want to use multithreading to make calls. Results these days only discovered the bug in the software, it seems that multithreading is not to use can be used, with bad will be very bad, leading to some inexplicable name of the bug.
I wrote a small example of a database, and also verified that the bug did exist. First of all, I created a table of two fields in the database, and two fields were m,n. where m I set the primary key, and manually added the data from 1 to 10, and then update the database by the way that the 10 data are updated.
int[] array = new INT[10] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int[] array1 = new int[10] {21,22,23,24,25,26,27,28,29,30};
private void update (int i)
{
string sqlstr = null;
SQLSTR + = "Update table_1 set n= '" +array1[i]+ "' where m= '" + array[i] + "'";
if (db. Execdatabysql (SQLSTR) >0)
{
MessageBox.Show ("zhixingchengong!");
}
}
Then add a button control to execute the call to this method, as follows:
private void Button1_Click (object sender, EventArgs e)
{
for (int i = 0; i <; i++)
{
if (i > 9) i = 9;
New Task (() = Update (i)). Start ();
}
}
The reason for adding if (I > 9) i = 9 is because the program inexplicably occurs beyond the index of the exception, it is very strange. Although added this, the program will still appear error, no solution. After executing this program, the discovery data only successfully updated 3 data.
I finally saw a multithreaded problem similar to mine.
Lambda expression vs. captured variable
As we can see, lambda expressions are the most powerful way to pass data to threads. However, be careful not to accidentally modify the captured variable (captured variables) after starting the thread. For example, consider the following example:
for (int i = 0; i <; i++)
New Thread (() = Console.Write (i)). Start ();
The output is not deterministic! That could be 0223557799.
The problem is that the variable I points to the same memory address throughout the loop. So, every thread when calling Console.Write, is using the variable that the value will be changed at run time!
Similar issues are described in the 8th chapter of the C # 4.0 In a nutshell "Captured Variables". This problem has nothing to do with multithreading, but rather is related to the rules for capturing variables in C # (sometimes not ideal in a for and foreach scenario).
The workaround is to use the temporary variable as follows:
for (int i = 0; i <; i++)
{
int temp = i;
New Thread (() = Console.Write (temp)). Start ();
}
The variable temp is localized for each iteration of the loop. So, each line routines captures a different memory address, which does not create a problem. We can use the simpler code to demonstrate the previous problem:
string text = "T1";
thread T1 = new Thread (() = Console.WriteLine (text));
Text = "T2";
Thread t2 = new Thread (() = Console.WriteLine (text));
T1. Start ();
T2. Start ();
Because two lambda expressions capture the same text variable, T2 is printed two times:
T2
T2
It seems that the thread is not used casually, I still have to slowly understand each problem, so that they become more and more strong, the program more and more beautiful.
A bug summary in C # code that causes the database to turn on multithreading for data updates.