In the previous articles, I have been writing LINQ. Why does multithreading appear here? The reason is that debuglzq encountered multithreading when writing a comprehensive LINQ demo, so he stopped and sorted it out. There are a lot of articles about multithreading in the garden, so LZ won't talk about the theory of multithreading. This blog post mainly uses the simplest example, this section summarizes the precautions for using multiple threads to call functions, focusing on application and memory.
1. Multithreading
Using system; using system. collections. generic; using system. LINQ; using system. text; using system. threading; namespace multithreading {class program {static void main (string [] ARGs) {console. writeline ("main thread start"); thread t = new thread (New threadstart (Showtime); // pay attention to the definition form of threadstart delegate T. start (); // when the thread starts, the control returns the main thread console. writeline ("the main thread continues executing"); // while (T. isalive = true); thread. sleep (1000); T. abort (); T. join (); // block the main thread until t terminates the console. writeline ("--------------"); console. readkey ();} static void Showtime () {While (true) {console. writeline (datetime. now. tostring ());}}}}
Note that the threadstart delegate is defined as follows:
It can be seen that the required function is: Return Value void, no parameter.
2. multi-threaded call of a function with parameters (two methods)
Using system; using system. collections. generic; using system. LINQ; using system. text; using system. threading; namespace multithreading 2 _ with parameter {class program {static void main (string [] ARGs) {console. writeline ("main thread start"); thread t = new thread (New parameterizedthreadstart (dosomething); // pay attention to the definition form of parameterizedthreadstart delegate T. start (New String [] {"hello", "world"}); console. writeline ("main thread continues execution"); thread. sleep (1000); T. abort (); T. join (); // block the main thread until t terminates the console. readkey ();} static void dosomething (Object s) {string [] STRs = s as string []; while (true) {console. writeline ("{0} -- {1}", STRs [0], STRs [1]) ;}}
Note that the parameterizedthreadstart delegate is defined as follows:
It can be seen that the requirements for input functions are: Return Value void, number of parameters 1, parameter type object
Using system; using system. collections. generic; using system. LINQ; using system. text; using system. threading; namespace multithreading 2 _ with parameter 2 {class program {static void main (string [] ARGs) {guest = new guest () {name = "hello ", age = 99}; thread t = new thread (New threadstart (guest. dosomething); // pay attention to the definition form T of the threadstart delegate. start (); thread. sleep (1000); T. abort (); T. join (); // block the main thread until t terminates the console. readkey () ;}// class guest {public string name {Get; Set ;}public int age {Get; Set ;} public void dosomething () {While (true) {console. writeline ("{0} -- {1}", name, age );}}}}
The threadstart delegate is used to encapsulate the method.
You can choose either of the two methods at will. The first one seems concise.
3. Thread Synchronization
There are many methods for thread synchronization: volatile, lock, interlock, monitor, mutex, readwritelock...
Here, lock is used to explain the problem: Where to synchronize, what to synchronize, and who to synchronize?
First, let's see what will happen if the synchronization fails:
The Code is the following code to remove the lock block.
Using system; using system. collections. generic; using system. LINQ; using system. text; using system. threading; namespace multithreading 3 _ synchronization 2 {class program {static object OBJ = new object (); // use static int balance = 500 for synchronization; static void main (string [] ARGs) {thread T1 = new thread (New threadstart (credit); t1.start (); thread t2 = new thread (New threadstart (debit); t2.start (); console. readkey ();} static void credit () {for (INT I = 0; I <15; I ++) {lock (OBJ) {balance + = 100; console. writeline ("after crediting, balance is {0}", balance) ;}} static void debit () {for (INT I = 0; I <15; I ++) {lock (OBJ) {balance-= 100; console. writeline ("after debiting, balance is {0}", balance );}}}}}
Of course, the above is the most basic method. You can use delegate, lambda expressions and other abbreviations. For details, refer to the next blog.
Conclusion: this is the case when multiple threads call a function. In winform, the control is bound to a specific thread to update the control from another thread and should not be directly called by members of the control. This is very useful, as described in the next blog.