Multi-threaded applications and multi-threaded Application Design
We are used to executing some additional time-consuming operations in a single thread, or performing operations on tasks that may impede other tasks. through the rational use of multiple threads, allows the client to get a faster response. By using separate threads to execute CPU-intensive and IO-intensive jobs, the user interface remains active. Of course, everything has two sides. Improper handling will lead to thread security issues.
Create and use threads
We often use the new keyword to create a thread. Below are some common thread methods.
// Create thread var Thread = new thread () => Console. writeLine (); // start thread. start (); // terminate thread. abort (); # The region method is out of date // suspends the thread. suspend (); // resume the suspended thread. resume (); # The endregion method is out of date.
Thread attributes
Some common attributes.
// Thread priority // thread. priority // thread status // thread. threadState // thread execution status // thread. isAlive // whether it is a background thread // thread. isBackground // thread name // thread. name
Thread priority
The CPU allocates a longer period of time to threads with higher priority.
Background thread
The background thread stops immediately after the last foreground thread stops.
Multithreading parameters and return values
Call methods with parameters in a thread
How can we call a method containing parameters in a thread?
The idea provided here is to create a new class, separate methods from parameters, and use parameters as attribute members of the class. The method can directly access the attributes.
Get the return value in the thread
Communicate through BackWorker.
Thread Synchronization
Advantages of multithreading: Each thread can be executed asynchronously. For form programs, time-consuming tasks can be executed in the background. The UI interface and controls always respond. For server programs, each request is processed by a thread.
When using multiple threads to access IO, network connections, memory, and other resources, you must pay attention to coordination. Because the thread cannot perceive the execution status of other threads during execution, when multiple threads access the same resource at the same time, unpredictable status results may occur.
Lock
Lock, which is the most common keyword to ensure thread security. The object required by lock must be of the reference type. The object is used by multiple threads to identify this region. When a thread enters this region, this area becomes locked, and other threads are waiting until the threads entering this area are executed (jumping out of the lock area ). This behavior is considered safe and synchronous.
Lock has several important knowledge points:
(1) avoid using the public type. For example, lock (this) will cause a deadlock, that is, multiple threads will wait for the release of the same object instance;
(2) avoid using the string type (string ). Because it will be saved by CLR, any specific string has only one instance, which means that strings with the same content are used in different places. In fact, they (strings) all point to the same instance object;
(3) It is recommended to be a private or protected member.
Monitor
Https://docs.microsoft.com/zh-cn/dotnet/csharp/programming-guide/concepts/threading/multithreaded-applications (reference)