Thread processing (2)

xiaoxiao2021-03-06  88

Advantages of multiple threads

In any case, it is the most powerful technology to increase the response speed to the user and process the required data for almost simultaneous completion. On a computer having a processor, multiple threads can achieve this effect by using a small period of time between the user event to process data in the background. For example, when another thread is recalculating other parts of the spreadsheet in the same application, the user can edit the spreadsheet. There is no need to modify, and the same application will greatly meet the needs of users when running on a computer with multiple processors. A single application domain can use multiple threads to complete the following tasks:

Communication with the web server and database via the network. Perform a lot of time to take up a lot of time. Distinguish tasks with different priorities. For example, high priority thread management time is key tasks, low priority threads perform other tasks. The user interface can still respond quickly when assigning the time to the background task.

Diffility of multiple threads

It is recommended that you use as few threads as possible, so you can minimize the use of operating system resources and improve performance. Thread processing also has resource requirements and potential collisions to consider when designing applications. These resource requirements are as follows:

The system will use memory for the context information required for the process, AppDomain object, and threads. Therefore, the processes that can be created, the number of AppDomain objects and threads can be limited by available memory. Tracking a large number of threads will take up a lot of processor time. If there are too many threads, most threads do not have a significant progress. If most current threads are in a process, the scheduling frequency of the thread in other processes will be low. Execution is very complicated using many thread-controlled code and may generate a lot of errors. Destroy the thread needs to understand the possible problems and process those issues.

Providing sharing access to resources can conflict. To avoid conflicts, you must synchronize or control access to shared resources for shared resources. If you fail to synchronize the access correctly in the same or different application domains, there will be some problems, including deadlocks and contention conditions, etc., which refers to both threads stop responding, and Waiting for the other party to complete; the contention of the contention refers to an abnormal results due to the accidental critical dependence of the execution time of the two events. The system provides synchronization objects that can be used to coordinate resource sharing between multiple threads. Reducing the number of threads makes the synchronous resources easier.

Resources that need to be synchronized include:

System resource (such as communication port). The resources shared by multiple processes (such as file handles). A single application domain accessed by multiple threads (such as global, static, and instance fields).

Thread processing and application design

In general, use the relatively short task that does not block other threads to handle multiple threads and do not need to perform any specific schedules for these tasks, use

Threadpool

Category is the simplest way. However, there are several reasons to create your own thread:

If you need to make a task have a specific priority. If you have tasks that may be running for a long time (and thus block other tasks). If you need to place the thread into a single-line unit (all ThreadPool threads are in the multi-threaded unit). If you need a stable identifier associated with the thread. For example, you should use a dedicated thread to abort the thread, hang it or find it by name.

转载请注明原文地址:https://www.9cbs.com/read-106632.html

New Post(0)