Address: http://www.cnblogs.com/DebugLZQ/archive/2012/11/11/2765487.html
Previous articlesArticleI 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 starts " ); Thread t = New Thread ( New Threadstart (Showtime )); // Note the definition form of threadstart Delegation T. Start (); // When the thread starts, the control returns the main thread Console. writeline (" Main thread continues execution " ); // While (T. isalive = true ); Thread. Sleep ( 1000 ); T. Abort (); T. Join (); // Blocks the main thread until t terminates. 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 requirements for passed functions are: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 ( " Start of main thread " ); Thread t = New Thread ( New Parameterizedthreadstart (dosomething )); // Pay attention to the definition form of the parameterizedthreadstart delegate T. Start (New String [] { " Hello " , " World " }); Console. writeline ( " Main thread continues execution " ); Thread. Sleep ( 1000 ); T. Abort (); T. Join (); // Blocks the main thread until t terminates. 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 )); // Note the definition form of threadstart Delegation T. Start (); thread. Sleep (1000 ); T. Abort (); T. Join (); // Blocks the main thread until t terminates. 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:
CodeThe following code removes 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 ();// Synchronous use Static Int Balance = 500 ; 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 );}}}}}
Conclusion: this is the case when multiple threads call a function.In winform, the control is bound to a specific thread and updated from another thread. Members of the control should not be directly called.This is very useful. I will talk about it in the next blog.