C # multithreading (I) Related Concepts of multithreading,
What is a process?
When a program starts to run, it is a process, including the memory and system resources used by the running programs and programs. A process is composed of multiple threads.
What is a thread?
A thread is an execution stream in a program. Each thread has its own proprietary register (Stack pointer, program counter, etc.), but the code zone is shared, that is, different threads can execute the same function.
What is multithreading?
Multithreading means that a program contains multiple execution streams, that is, a program can run multiple different threads to execute different tasks at the same time, that is to say, a single program is allowed to create multiple parallel threads to complete their respective tasks.
Advantages of multithreading:
CPU utilization can be improved. In a multi-threaded program, when a thread has to wait, the CPU can run other threads instead of waiting, which greatly improves the program efficiency.
Disadvantages of multithreading:
The thread is also a program, so the thread needs to occupy the memory. The more threads occupy the memory, the more;
Multithreading requires coordination and management, so it requires CPU time to track threads;
Inter-thread access to shared resources will affect each other, and the problem of competing to share resources must be solved;
Too many threads will lead to too complex control, which may lead to many bugs;
Next, we will discuss the multithreading mechanism in C # programming. In order to save the tedious steps for creating a GUI and more clearly approach the essence of the thread, all subsequent programs are Console programs and the final Console of the program. readLine () is used to stop the program midway through, so that you can see the output in the execution process clearly.
Any program must have at least one main thread during execution.
A thread example with an intuitive impression:
// SystemThread. csusing System; using System. threading; namespace ThreadTest {class RunIt {[STAThread] static void Main (string [] args) {Thread. currentThread. name = "System Thread"; // Name the current Thread "System Thread" Console. writeLine (Thread. currentThread. name + "'status:" + Thread. currentThread. threadState); Console. readLine ();}}}
The output is as follows:
System Thread's Status: Running
Here, we get the Thread currently being executed through the static attribute CurrentThread of the Thread class, assign the value "System Thread" to its Name attribute, and finally output its current state (ThreadState ).
The so-calledStatic attributesIt is the public attribute of all objects in this class. No matter how many instances of this class are created, the static attribute of the class has only one in the memory. It is easy to understand why CurrentThread is static-although multiple threads exist at the same time, the CPU can only execute one of them at a certain time point.
In the program header, we use the following namespace:
using System; using System.Threading;
In. net framework class library, all classes related to multithreading applications are stored in the System. Threading namespace. If you want to use multithreading in your application, you must include this class.
We use the Thread class provided to create and control threads. The ThreadPool class is used to manage Thread pools. (In addition, it provides a mechanism to solve actual problems such as thread execution arrangements, deadlocks, and inter-thread communication .)
The Thread class has several important methods, which are described as follows:
Start (): Start the thread;
Sleep (int): a static method that pauses the specified number of milliseconds of the current thread;
Abort (): This method is usually used to terminate a thread;
Suspend (): This method does not terminate the unfinished thread. It only suspends the thread and can be recovered later;
Resume (): Resume the execution of threads suspended by the Suspend () method;
C # multi-thread Learning Series:
C # multithreading (I) concepts related to multithreading
Http://www.cnblogs.com/zpx1986/p/5571506.html
C # multi-thread Learning (2) how to manipulate a thread
Http://www.cnblogs.com/xugang/archive/2008/04/06/1138841.html
C # multithreading (3) producer and consumer
Http://www.cnblogs.com/xugang/archive/2008/03/23/1118594.html
C # multithreading (4) Automatic Management of multithreading (thread pool)
Http://www.cnblogs.com/xugang/archive/2008/03/23/1118584.html
C # multi-thread Learning (5) automatic management of multiple threads (timer)
Http://www.cnblogs.com/xugang/archive/2008/03/23/1118537.html
C # multi-thread Learning (6) mutually exclusive objects
Http://www.cnblogs.com/xugang/archive/2008/03/23/1118530.html