Home > Article > Backend Development > A preliminary study on the multi-threading mechanism of C# (1)
1. The concept of multi-threading
Windows is a multi-tasking system. If you are using Windows 2000 and above, you can view the programs and processes currently running on the system through the Task Manager. What is a process? When a program starts running, it is a process. The process refers to the running program and the memory and system resources used by the program. A process is composed of multiple threads. A thread is an execution flow in the program. Each thread has its own private register (stack pointer, program counter, etc.), but the code area is shared, that is, different Threads can execute the same function. Multithreading means that a program contains multiple execution streams, that is, multiple different threads can be run simultaneously in one program to perform different tasks, which means that a single program is allowed to create multiple parallel execution threads to complete their respective tasks. A good example of multithreading is the browser, where you can scroll pages while downloading Java applets or images, play animations and sounds when accessing new pages, print files, etc.
The benefit of multi-threading is that it can improve CPU utilization - no programmer wants his program to have nothing to do all the time. In a multi-threaded program, when one thread has to wait, the CPU can run other thread instead of waiting, which greatly improves the efficiency of the program.
However, we must also recognize the adverse aspects of threads themselves that may affect system performance in order to use threads correctly:
Threads are also programs, so threads need to occupy memory. The more threads, the more memory they occupy
Multiple threads require coordination and management, so CPU time is needed to track threads
The access to shared resources between threads will affect each other, and the problem of competing for shared resources must be solved
Too many threads will cause the control to be too complex, which may eventually cause many bugs
Based on We can use a metaphor to deepen our understanding of the above understanding. Suppose there is a company with many employees who perform their own duties. Then we can think of this normally operating company as a process, and the employees in the company are threads. A company must have at least one employee. Similarly, a process must have at least one thread. In a company, you can have one employee do everything, but the efficiency is obviously not high, and it is impossible for a one-person company to grow; a program can also use only one thread to do things. In fact, some outdated This is true for languages such as fortune and basic, but like a one-person company, the efficiency is very low. If you make a large program, the efficiency is even lower - in fact, there is almost no single-threaded commercial software now. The more employees there are in the company, the more the boss has to pay them, and he has to spend a lot of energy to manage them and coordinate the conflicts and interests between them; the same is true for the program, the more threads, the more resources are consumed, requiring CPU time is used to track threads, and problems such as deadlock and synchronization must be solved. In short, if you don't want your company to be called a "skin bag company", you have to have a few more employees; if you don't want your program to look childish, introduce multi-threading into your program!
This article will discuss the multi-threading mechanism in C# programming, and solve problems such as thread control and communication between multi-threads through some examples. In order to save the tedious steps of creating a GUI and more clearly approach the nature of threads, all the following programs are console programs. The Console.ReadLine() at the end of the program is to stop the program midway so that you can clearly see the execution process. Output.
Okay, without further ado, let’s experience multi-threaded C#!
2. Manipulate a thread
When any program is executed, there is at least one main thread. The following small program can give readers an intuitive impression:
[CODE]
//SystemThread.cs
using System / /give current The thread is named "System Thread"
Console.WriteLine(Thread.CurrentThread.Name+"'Status:"+Thread.CurrentThread.ThreadState);
}
}
}
[/CODE]
What do you see after compiling and executing? Yes, the program will produce the following output:
System Thread's Status: Running
Here, we obtain the currently executing thread through the static property CurrentThread of the Thread class, and assign "System Thread" to its Name property. Finally, its current state (ThreadState) is output. The so-called static properties are properties common to all objects of this class. No matter how many instances of this class you create, there is only one static property of the class 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 moment.
As demonstrated in the above program, we create and control threads through the Thread class. Notice that in the head of the program, we use the following namespace:
[CODE]
using System;
using System.Threading;
[/CODE]
In the .net framework class library, all Classes related to multi-threading mechanism applications are placed in the System.Threading namespace. It provides the Thread class for creating threads, the ThreadPool class for managing thread pools, etc. In addition, it also provides mechanisms to solve practical problems such as thread execution arrangements, deadlocks, and inter-thread communication. If you want to use multithreading in your application, you must include this class. The Thread class has several crucial methods, described as follows:
Start(): Start the thread
Sleep(int): Static method, pause the current thread for the specified number of milliseconds
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 resumed later.
Resume(): Resumes the execution of the thread suspended by the Suspend() method
The above is the content of the preliminary exploration of C#’s multi-threading mechanism (1). For more related content, please pay attention to the PHP Chinese website (www.php.cn)!