Multithreading can effectively help you achieve higher performance and higher scalability of applications. But be careful when using this technology. This article is the beginning of a series of articles on tools and technical issues related to thread technology. First, I will introduce the thread concept, summarize some common structures, and finally introduce their usage.
Two sides of the thread
Writing multi-threaded programs in Java is not difficult. This is a good thing and a bad thing. When Microsoft developed C #, they copied the ease-of-use dilemma to the entire new platform. At the same time, C # has more program primitives than Java, but the basic java primitives of the thread object and synchronization monitor are sufficient in terms of form and function to provide powerful Thread Programming capabilities. Therefore, be careful before deciding to adopt multithreading technology for applications.
Why not use multithreading?
Remember, when deciding whether to adopt multithreading technology, unless you are playing with code, do not simply use thread-based programming because multithreading programming is cool. Multi-threaded programming is too fashionable. If you are not careful, your boss will be fascinated sooner or later, and then you will die. Second, do not use multithreading easily because it allows the program to run faster, unless you can prove that single-thread implementation is really slow. Finally, before taking the liberty to plunge into the multi-thread mechanism, I would like to recall an apartment model provided by Microsoft, that is, the object is written as a single-threaded structure and runs in a multi-threaded environment. To put it bluntly, you do not have to adopt multi-threaded encoding. However, apartment models are another topic.
If not, multi-threaded programming will inevitably open Pandora's box (meaning it can cause numerous troubles ). The repeatability is not obvious, the program garbage is generated, and the counter is not correctly added. Your application may also be suspended suddenly. For example, a database connection may unexpectedly close or become overloaded. A major headache for advanced developers is to solve the thread problem. These major problems don't take time to solve, and they have a serious negative impact on the product delivery date and product reliability.
Why multithreading?
If your application requires the following operations, you can consider the multithreading mechanism during programming:
Continuous operations can take a long time to complete.
Parallel Computing
It takes a lot of time to wait for the network, file system, user, or other I/O responses
Therefore, make sure that the preceding three situations exist in your application before you start.
If your code runs fast enough, but you think you can make it run faster (assuming you do have this skill), I advise you not to accept this temptation. If you are not sure about the concurrency of the program's computing operations (for example, the concurrent database changes for the same data table-when your database reaches the data table Level Lock), then think about other methods. In addition, if you do not know whether the application spends too much time waiting for input or output, you must first understand the time-consuming situation. In fact, it takes much longer to start three threads to calculate the circumference rate in one thousandth step than to repeat three times in the same thread. Why is this failure? The reason is that, although 2nd Parallel Computing items are indeed available, the designers just ignore the above 3rd standards: There is no idle cycle during a computing period that can be used by parallel computing.
If you are writing a program for a parallel computer equipped with multiple processors, the above rules are exceptional in this case, you can benefit greatly from the software performance through proper parallel operation design-even if each operation is extremely greedy for CPU time.
Basic thread management tools
I have issued a considerable warning for multithreaded programming just now, and also made suggestions for when to use or not to use multithreading. Next I will elaborate on some tools that can be used by multithreaded programming.
Thread object
The. Net Library provides an object named system. Threading. Thread, which represents a single thread. You can start the thread and try to complete the task of the thread when the current thread continues running. This is too much help for applications that need to print documents or save large files but want to get user confirmation requests and return control to users. We demonstrated this mechanism through program list.
Thread Programming includes thread creation, suspension, wakeup, termination, and other operations.
The specific test code is as follows:
Using system; <br/> using system. collections. generic; <br/> using system. LINQ; <br/> using system. text; <br/> using system. threading; </P> <p> namespace threadtest <br/>{< br/> class Program <br/>{< br/> static void main (string [] ARGs) <br/>{< br/> // thread = new thread (New threadstart (helloworld); <br/> // thread. start (); // start a thread </P> <p> // thread. priority = threadpriority. highest; </P> <p> // If (Th Read. isalive) // determine the thread status <br/> // {<br/> // console. writeline ("thread is alive"); <br/> // console. writeline (thread. threadstate. tostring (); <br/>//}</P> <p> // If (thread. threadstate = threadstate. running) // suspends a thread <br/> // {<br/> // thread. sleep (1000); // pause the thread <br/> // console. writeline ("thread is suspend"); <br/> // console. writeline (thread. threadstate. tostring (); <br/>//}</P> <p> // If (thread. threa Dstate = threadstate. suincluded) // wake up a thread <br/> // {<br/> // thread. resume (); <br/> // console. writeline ("thread is resume"); <br/> // console. writeline (thread. threadstate. tostring (); <br/>//}</P> <p> // thread. abort (); </P> <p> // If (! Thread. isalive) <br/> // {<br/> // console. writeline ("thread is over"); <br/> //} <br/> thread thread1 = new thread (New threadstart (helloworld )); <br/> thread thread2 = new thread (New threadstart (hellodotnet); <br/> thread1.start (); <br/> thread2.start (); <br/> console. read (); <br/>}</P> <p> protected static void helloworld () <br/>{< br/> for (INT I = 1; I <3; I ++) <br/>{< br/> console. writelin E ("Hello world! "); <Br/> thread. sleep (1000); <br/>}</P> <p> protected static void hellodotnet () <br/> {<br/> for (INT I = 1; I <3; I ++) <br/> {<br/> console. writeline ("Hello DOTNET! "); <Br/> thread. Sleep (300); <br/>}< br/>
Another Code is as follows:
Using system; <br/> using system. threading; </P> <p> // simple threading scenario: Start a static method running <br/> // on a second thread. <br/> public class threadexample <br/> {<br/> // The threadproc method is called when the thread starts. <br/> // It loops ten times, writing to the console and yielding <br/> // the rest of its time slice each time, and then ends. <br/> Public static void threadproc () <br/> {<br/> for (INT I = 0; I <10; I ++) <br/> {<br/> console. writeline ("threadproc: {0}", I); <br/> // yield the rest of the time slice. <br/> thread. sleep (0); <br/>}</P> <p> Public static void main () <br/>{< br/> console. writeline ("main thread: Start a second thread. "); <br/> // The constructor for the Thread class requires a threadstart <br/> // delegate that represents the method to be executed on the <br/> // thread. C # simplifies the creation of this delegate. <br/> thread t = new thread (New threadstart (threadproc); <br/> // start threadproc. on a uniprocessor, the thread does not get <br/> // any processor time until the main thread yields. uncomment <br/> // the thread. sleep that follows T. start () to see the difference. <br/> T. start (); <br/> // thread. sleep (0); </P> <p> for (INT I = 0; I <4; I ++) <br/>{< br/> console. writeline ("main thread: do some work. "); <br/> thread. sleep (0); <br/>}</P> <p> console. writeline ("main thread: Call join (), to wait until threadproc ends. "); <br/> T. join (); <br/> console. writeline ("main thread: threadproc. join has returned. press enter to end program. "); <br/> console. readline (); <br/>}< br/>