In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "what are the knowledge points of C# multithreading". In daily operation, I believe that many people have doubts about the knowledge points of C# multithreading. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the questions of "what are the knowledge points of C# multithreading?" Next, please follow the editor to study!
C# multithreading learning the concept of multithreading:
C# multithreading means that a program contains multiple execution streams, that is, multiple different threads can be run simultaneously in a program to perform different tasks, that is, a single program is allowed to create multiple threads of parallel execution to complete their respective tasks.
Benefits of C# multithreading:
It can improve the utilization of CPU. In multithreaded programs, when a thread has to wait, CPU can run other threads instead of waiting, which greatly improves the efficiency of the program.
Disadvantages of C# multithreading:
Threads are also programs, so threads need to occupy memory, and the more threads take up memory, the more memory they occupy; multithreads need to coordinate and manage, so they need CPU time 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 lead to too complex control, which may eventually lead to a lot of Bug.
Next, we will discuss the multithreading mechanism in C# programming. In order to omit the tedious steps of creating GUI and approach the essence of threads more clearly, all the following programs are console programs, and the Console.ReadLine () of the program * * is to stop the program in order to see the output during execution.
When any program is executed, there is at least one main thread.
An intuitive example of C# multithreading:
Using 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 currently executing thread through the static property CurrentThread of the Thread class, assign the value "System Thread" to its Name property, and * * output its current state (ThreadState).
Static properties are properties that are common to all objects in the class. No matter how many instances of the class you create, there is only one static property in memory. It's easy to understand why CurrentThread is static-although there are multiple threads at the same time, CPU can only execute one of them at a time.
At the head of the program, we use the namespace:
Using System; using System.Threading
In. Net framework class library, all classes related to multithreaded applications are placed in the System.Threading namespace. If you want to use multithreading in your application, you must include this class.
We create and control threads through the Thread class provided, and the ThreadPool class is used to manage thread pools, and so on. (in addition, it also provides a mechanism to solve practical problems such as thread execution scheduling, deadlock, inter-thread communication and so on. )
There are several critical methods for the Thread class of C # multithreaded learning:
Start (): start the thread
Sleep (int): static method that pauses the number of milliseconds specified by the current thread
Abort (): this method is usually used to terminate a thread
Suspend (): this method does not terminate an unfinished thread, it just suspends the thread and can resume later
Resume (): resumes execution of threads suspended by the Suspend () method.
At this point, the study of "what are the knowledge points of C# multithreading" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.