In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces the example analysis of asynchronous multithreading in C#, which is very detailed and has a certain reference value. Friends who are interested must finish it!
Process, thread 1. Process
First of all, what is a thread? That is, when an application is running, the synthesis of resources is a process. As you can see in the Windows task manager, there are running processes one by one.
two。 Thread
A thread is the smallest unit of execution flow. Threads are invisible, but they can, for example, the Windows Task Manager: 272 processes are running, and 272 processes are running 3909 threads, that is, a process can have multiple threads.
Time-sharing, slicing
Now there is a strange appearance, CPU is really too fast, memory graphics card other hardware resources actually can not keep up with the speed of CPU, so the concept of slicing came into being. From a micro point of view, in the past, many computers were single-core and could only execute one thread at a time. According to this reason, why can our computers run many applications at the same time? But from a macro point of view, it is concurrent, and multiple applications are executed at the same time, so we can not only mine but also finish spider cards and listen to music. This is sharding, which creates a context. Assuming that the current minesweeping thread is executed and the spider card thread is executed next time, CPU will save the minesweeping thread context and switch it to spider card thread to schedule back and forth. From a macro point of view, it is concurrent.
This additional knowledge, multi-CPU multi-core, itself can complete the calculation of multiple threads, can work independently. 4 cores and 8 threads, the core is the physical core, the thread is the virtual core, and each core can be sliced and done concurrently.
Synchronous, asynchronous
What we developers often say about synchronization and asynchronism is actually a description of method execution. Because the programming language itself has no threads, it can only apply to the operating system for threads to execute the code.
Synchronization method, the code execution of the first line to the last line of execution to the end, after the completion of the first line to enter the next second line to the last line, this is synchronous, blocking.
The asynchronous method, which does not wait for the current line to complete, will proceed to the next line of code execution, which is non-blocking.
Asynchronous, multithreaded
Multithreading means that multiple execution streams are executed at the same time. In C #, multi-threading means that multiple concurrent Thread starts multiple threading tasks, which may be performed by using the multi-core CPU or single-core CPU fragments.
Asynchronism is actually hardware asynchronism, which is not easy to understand. Here, take file writing as an example.
In the case of multithreading, the thread will participate in the work from the beginning to the end of the file writing, that is, the CPU will handle the write file operation, and the thread will wait for CPU to write the file to the disk until it is finished.
In the case of asynchronism, the thread will send an instruction to the CPU to hand over the file stream to the operating system, and the thread can be busy with other things, that is, making use of the characteristics of the hardware, send an instruction to the operating system to complete the file writing, and the thread will perform other tasks. After the CPU has finished writing, the thread will send an instruction back to notify the thread.
Some students will ask how to write this asynchronous, in fact, it is impossible to write this in the program, this is the bottom of the operating system, it can be called directly in WPF. Asynchronous multithreading often mentioned in C # refers to ThreadPool and Task, which are all based on Thread, but encapsulated by C # language.
Asynchronous multithreading efficiency
Speaking of asynchronism, many people know that synchronous methods are slow and asynchronous multithreading is fast. This is what everyone thinks in their hearts. But how much has the efficiency improved?
Let's take a look at the following program, define a common method Sum to do some CPU-intensive operations, and then execute the Sum method in the Main method, synchronous and asynchronous, respectively. That is, the same task is run five times with a single thread (main thread), and then five threads are opened with multiple threads to execute respectively.
Public static void Sum (int f) {Console.WriteLine ($"{f} th start: {DateTime.Now.ToLongTimeString ()}, Thread: {Thread.CurrentThread.ManagedThreadId}"); decimal s = 0; for (int I = 0; I < 10000000000; iTunes +) {s = s + I;} Console.WriteLine ($"{f} th end: {DateTime.Now.ToLongTimeString ()}, Thread: {Thread.CurrentThread.ManagedThreadId}") } static void Main (string [] args) {Console.WriteLine ($"synchronous start: {DateTime.Now.ToLongTimeString ()}, Thread: {Thread.CurrentThread.ManagedThreadId}"); for (int I = 0; I < 5; iTunes +) {Sum (I);} Console.WriteLine ($"synchronous end: {DateTime.Now.ToLongTimeString ()}, Thread: {Thread.CurrentThread.ManagedThreadId}"); Console.WriteLine () Console.WriteLine ($"Asynchronous start: {DateTime.Now.ToLongTimeString ()}, Thread: {Thread.CurrentThread.ManagedThreadId}"); for (Asynchronous end: {DateTime.Now.ToLongTimeString ()}, Thread: {Thread.CurrentThread.ManagedThreadId}); Console.ReadKey ();}
Starting the program, you can see that the synchronous method 1 thread executed 2 minutes 40 seconds, the asynchronous method 5 threads executed 52 seconds, it is obvious that the asynchronous method is more than four times faster than the synchronous method.
Speaking of which, some students may find that if one thread is synchronized and five threads are asynchronous, why not increase the efficiency fivefold and increase linearly? In fact, the improvement of asynchronous efficiency is limited by resource constraints and context switching costs.
As we mentioned above, CPU time-sharing and slicing, even a single core can run multiple programs at the same time, which not only requires the horse to run fast, but also requires the horse not to eat grass. How is it possible? this is the management cost of context switching.
Resource constraints, asynchronous efficiency is not high and may not have enough resources. As follows, when we start the resource manager and start the program, we can see that the resources used by CPU during synchronization are not high but the execution time is long. When there are multiple threads, the CPU reaches 100% but the execution time is short, which is a resource-for-time strategy.
Multithreading disorder
Because the time-sharing and slicing of the computer will make the multi-thread out of order. That is, start disorder, execute disorder, and end disorder.
The startup is out of order, the thread is of the operating system, and C# does not have a thread. C# needs to apply for a thread to the operating system, assuming that the thread is applied to the operating system at the same time or in an orderly manner, but the operating system does not give it to the thread in order. It is possible that the thread who applies later gets the thread first, or the thread that applies first.
After the execution is out of order, and after the program gets the thread of the operating system fragment, it is very common for us to execute the task after we are lucky and apply for it first and then execute it after we have bad luck.
The end of disorder is affected by execution disorder, time-sharing luck, and the amount of tasks. Starting execution disorder has already been said; time-sharing and slicing luck, when my CPU executes a task, it is not after the task is executed that the context will be changed to execute other tasks, but can be paused to execute other tasks at any time; the task volume is that the tasks done are more or less different, even if the execution starts at the same time, the completion time must be different.
The above is all the contents of the article "sample Analysis of Asynchronous Multithreading in C#". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!
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.