Analysis of the multi-threaded programming (ZT) Send Station under. NET: BBS Shuimu Tsinghua Station (Mon Nov 24 16:42:24 2003) http://www.aspcool.com/lantmu/browse...&bbsuser=csharp Multithreading is the characteristics of many operating systems, which greatly improves the operational efficiency of the program, so multi-threaded programming technology is widely concerned for programmers. At present, Microsoft's .NET strategy is further promoted, and various related technologies are accepting the majority of programming people, and the multi-thread programming technology in .NET has a considerable status. This article I introduce you to the basic methods and steps of multithreading programming under .n. Starting new threads Creating a new thread under .NET is very easy, you can start a new thread through the following statement: thread thread = New Thread (New Threadstart (threadfunc)); thread.start (); first The statement creates a new THREAD object and indicates a method of this thread. When the new thread starts, the method is called executed. This thread object calls the thread method to call by a case-secure method through an instance of a System..threading.ThreadStart class. The second statement officially starts the new thread, once the method start () is called, the thread is kept in a "Aliv E" state, you can determine if it is "alive" by reading its isalive property status. The following statement displays the method that hangs the thread if a thread is in the "Alive" state: if (thread.isalive) {thread.suspend ();} However, please note that the start () method of thread object is just startup This thread does not guarantee that its thread method thr EADFUNC () can be implemented immediately. It simply ensures that the thread object can be assigned to the CPU time, and the actual execution is also determined by the operating system according to the processor time. A thread method does not contain any parameters, and no value is returned. Its naming rules are the same as the name rules of the general function. It can either static (static) or nonStatic. When it is executed, the corresponding thread is over, and the IsAlive property of its thread object is set to false. Below is an example of a thread method: public static void threadfunc () {for (int i = 0; i <10; i ) {Console.writeline ("ThreadFunc {0}", I);}} front desk thread and background thread COMMON LANGUAGE RUNTIME, CLR can distinguish between two different types of threads: front desk threads and background threads. The difference between the two is: The application must run all the front desk threads to exit; and for the background thread, the application can not consider whether it has been running directly, all the background threads will be exited when the application exits. Auto end. A thread is the front desk thread or the background thread can be determined by its IsBackground properties. This attribute is readable and writable. Its default value is false, which means a thread defaults to the front desk thread. We can set its ISB Ackground property to True so that it becomes a background thread. The following example is a console program that starts 10 threads at first, and each thread runs for 5 seconds.
Since the ISBackground attributes of the thread defaults to False, they are all reception threads, so although the main thread of the program is running soon, the program is running to all started threads will end. The sample code is as follows: use system; use system.threading; class myapp {public static void main () {for (int i = 0; i <10; i ) {thread thread = new Thread (New Threadstart (threadfunc); thread .Start ();}} private static void threadfunc () {datetime start = datetime.now; while ((DateTime.now - start). Seconds <5);}} Next, we will slightly modify the above code, Each thread is set to True, each thread is a background console. Then just overhead of the program's main thread, the entire program is over. The sample code is as follows: use system; use system.threading; class myapp {public static void main () {for (int i = 0; i <10; i ) {thread thread = new Thread (New Threadstart (threadfunc); thread .Isback.start ();}} private static void threadfunc () {datetime start = datetime.now; while ((DateTime.now - start). Seconds <5);}} Since the front desk thread and background thread Have this difference, then how do we know how to set a thread's isbackground property? Here is some basic principles: For some threads running in the background, these threads do not have to run when the program ends, then these threads should be set to the background thread. For example, a program launches a large number of arithmetic threads, but once the program ends, the thread lost its meaning, then that thread should be a background thread. For some threads that serve the user interfaces are often set to the front desk thread, because even if the main thread of the program is over, the threads of the other user interfaces are likely to continue to display the relevant information, so they cannot be immediately terminated. Here I only give some principles, and the actual use often requires further careful consideration of the programmer. The thread priority once a thread starts running, the thread scheduler can control the CPU time it obtained. If a hosted application is running on a Windows machine, the thread scheduler is provided by Windows. On other platforms, the thread scheduler may be part of the operating system, and naturally it may be part of the .NET framework. However, we don't have to consider how the thread's scheduler is generated, we only need to know the priority of the thread we can make the thread different CPU time. The priority of threads is controlled by Thread.Priority property, which contains: ThreadPriority.Highest, ThreadPriority.Abovenormal, ThreadPriority.Normal, ThreadPriority.Belownormal, and ThreadPriority.lowest. From their name, we can naturally know their priority, so there is not much introduction here. The default priority of thread is ThreadPriority.Normal.
In theory, threads with the same priority will obtain the same CPU time, but when actually executed, the thread blocking or improvement of the operating system in the message queue will result in a thread having the same priority. CPU time. However, this difference can still be ignored from the moment. You can change the priority of a thread by changing the following methods. Thread.Priority = ThreadPriority.abovenormal; or: thread.priority = threadpriority.belownormal; You can improve a thread priority by the first statement above, then get more CPU time accordingly; The second statement that you reduce the priority of that thread, so it will be assigned to a few CPU time than the original. You can change its priority before running in a thread or at any time during its running process. In theory, you can also set the priority of each thread, but a thread that is too high in priority will often affect the operation of other threads, and even affect the operation of other programs, so it is best not to set up threads at will. level. Suspending threads and restart thread The Thread class provides two ways to suspend the thread and restart threads, that is, thread.suspend can suspend a running thread, and thread.resume can make the thread continue to run. Unlike the Windows kernel, .NET framework is not a number of hanging number of threads, so no matter how long you hang up, as long as you call thread.resume once, you can restart the hanging thread. The Thread class also provides a static Thread.Sleep method that allows a thread to automatically hang a certain time and then automatically restart. A thread can call the Thread.Sleep method inside itself, and can also call the Thread.Suspend method inside itself, but must call its thread.ResuMe method to start again. Is this very easy to think? The following example shows how to use Thread.Sleep method: while (); thread.sleep (5000); Thread termination: thread.abort (); let us explain how the Abort () method works. Because all managed threads are managed at the public language, they can throw an exception within each thread. The Abort () method throws a ThreadAbortException exception in the target thread to lead to the termination of the target thread. However, the abort () method is called, the target thread may not be terminated immediately. Because as long as the target thread is calling the unmanaged code and has not returned, the thread will not terminate immediately. If the target thread is invoking the unmanaged code and falls into a dead cycle, the target thread will not terminate. However, this situation is just some special cases, more cases are the target thread in the calling code, once the abort () is called, the thread is terminated immediately. In practical applications, a thread terminates another thread, but it is often necessary to wait for that thread to completely terminate it can continue to run, so we should use its Join () method.