Generally, software that requires user interaction must respond to user activities as quickly as possible to provide a rich user experience, but at the same time, it must execute the necessary calculations to present the data to the user as quickly as possible. In this case, multithreading can be used for implementation.
Multithreading is a very complex mechanism. If you cannot understand the meaning of this sentence at this time, you can try to read three books at the same time. First, read the first chapter of the first book, then read the first chapter of the second book, read the first chapter of the second book, read the second chapter of the first book, and so on. Readers do not need to experience the complexity of multithreading for a long time.
Since multithreading is so complex, how does it work in the operating system? In fact, there are also differences in the running methods of multithreading in C # In each operating system. The author focuses on the operating mode of multithreading in windows. Windows is a multitasking operating system, which is a process. A process is a program containing its own address, each independently executed program is called a process, that is, an ongoing program. In the system, you can allocate a limited CPU usage time (or CPU time slice) to each process ), the CPU executes a process in the fragment time, and then the next time slice jumps to another process for execution. Because the CPU conversion speed is fast, it seems that each process is executed at the same time.
Figure 1 shows the Execution Mode of the Windows operating system.
Figure 1 Execution Mode in Windows
1. Advantages of Multithreading
To increase the response speed to users and process the required data in almost the same time, multithreading is the most powerful technology. On a computer with a processor, multithreading can achieve this by processing data in the background for a very small period of time between user events. For example, you can edit a workbook when another thread is recalculating other parts of a workbook in the same application by using multithreading.
A single application domain can use multiple threads to complete the following tasks.
Communicates with web servers and databases through the network;
Perform operations that take a large amount of time;
Differentiate tasks with different priorities;
This allows you to quickly respond to background tasks by assigning time to the user interface.
2. disadvantages of Multithreading
Multithreading is advantageous, but it also has disadvantages. We recommend that you do not use too many threads in the program. This can minimize the use of operating system resources and improve the performance.
If multithreading is used in the program, the following problems may occur.
The system uses memory for context information required by processes, appdomain objects, and threads. Therefore, the number of processes, appdomain objects, and threads that can be created will be limited by the available memory;
Tracking a large number of threads will take a lot of processing time. If there are too many threads, most of them will not produce significant progress. If most of the current threads are in one process, the scheduling frequency of threads in other processes will be very low;
Using many threads to control code execution is complex and may cause many bugs;
To destroy a thread, you must understand the possible problems and handle them.