In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Editor to share with you the example analysis of C# asynchronous programming, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's learn about it!
Asynchronous programming is increasingly used in dealing with concurrency, and the above sentence is said to distinguish between multithreaded programming. As you all know, in fact, the core goal of asynchronous programming is concurrent processing. But there are often some frustrating statements and questions, such as, can asynchronous programming improve application performance? Can he shorten the time I take to deal with the task? Does he block threads? If you don't block the thread, why don't the breakpoint continue to execute down, my brother! Where is the thread released? I have read less you do not lie to me, threads are released how to run the program? I used Ajax at the front desk, but is it necessary to use Async in the background? Maybe if, as a driver, you see a problem, you have to ┑ the ┍.
Multithreaded scenario understanding
Maybe at some point, you want to improve the execution speed of the application and get a result as soon as possible. At this time, it is definitely not Async and Task that should be chosen. For example, when you and your wife go shopping at the supermarket on the weekend, as soon as you enter the supermarket, you find that there are dozens of people in each checkout line, so you use multithreading, you go to the queue, you go forward alone, your wife is on the other side of the store, and when you are almost to the cashier, your wife comes and pushes the shopping cart to you, so you check out and go home directly. Although this behavior is uncivilized, it is multithreaded and has nothing to do with asynchronous programming.
Understanding of asynchronous programming scenarios
So what is the situation of asynchronous programming and what problems can be solved? You and your wife opened a bakery, and at first only the two of you served the customers. Unexpectedly, the opening of the new store is so popular that there is a customer every minute, and it takes two minutes to bake a piece of bread. For each customer, you take a piece of bread to the back kitchen oven to bake, and you and your wife have to spend two minutes waiting for their respective ovens to finish the task. But you waited for these two minutes, there are two more customers, at this speed, simply can not meet the needs of customers ah! You have found the problem with you and your wife: both you and your wife are blocked by the time spent in the oven!
In order to solve the problem of congestion, you and your wife bought two more ovens, and to avoid unserved new customers, every time you put the bread in the oven, mark which customer it belongs to and return immediately, ready to receive new customers, when more customers come, receive them immediately, and send the new bread to another oven and mark it, and immediately return to wait for service to others. After the bread is baked, the oven will give a "ding". Note that when this signal arrives, you don't have to go to the back kitchen toaster to get the bread, but you and your wife who is not busy to get it. After this treatment, the high number of concurrent customers will be handy for you. You and your wife, as two threads, can constantly return to the customer in a non-blocking form (not waiting for the oven). But it is important to pay attention to the concept of non-blocking, it is not to let your program continue to execute down. As far as toast is concerned, one of your toast methods is as follows:
1. Send the bread to the oven 2. The oven handles the bread and gives you the results. Get the bread and deliver it to the customer. So the concept of "non-blocking" does not allow you to go straight to the third step. During the non-blocking period, there is no thread in your method, and this method still has to wait for time, waiting for a signal to wake you or your wife at some point in the future, when the method resumes execution. So the execution time of the program remains the same, and what is optimized is the ability to handle concurrency and the throughput of your store (server).
Look at the code and understand.
Asynchronous programming should be suitable for IO-intensive scenarios and non-CPU computing-intensive scenarios. It is well known that threads are scheduled by CPU, and if you are a quad-core CPU, performance will be best when you have four threads in your thread pool and the process allocates one thread to each virtual CPU. CPU can be used efficiently without having to switch context loss performance back and forth. If you think about it, in CPU-intensive scenarios, CPU is meant to take up your threads, and asynchronous programming is of no use at this time. In the IO scenario, however, the file IO operates the disk driver in kernel mode from API in win32 user mode to windows kernel mode. In the meantime, your thread blocks in the driver's response. In asynchronous programming, after your operation is notified to the disk driver, the thread returns immediately instead of waiting. At some point in the future, the driver processing ends, the processing result is put into the CLR thread pool queue, and the state machine is restored. Any thread in the thread pool fetches the result, and the method continues to execute downward. The same is true in network IO, except that the driver becomes a network driver. Take a look at the following code:
Public static async Task DoSomeAsync () {using (var client = new HttpClient ()) {var result = await client.GetAsync ("http://stackoverflow.com/questions/37991851/jenkins-configure-page-not-loading-version1-651-3-chrome-browser") .Result.Content.ReadAsStringAsync (); Console.WriteLine (result); / / do some other operations var res = 1 + 1 / /-return "";}}
When compiling, DosomeAsync will be compiled into a state machine method, never mind what the state machine is, you can think of it as a black box. When GetAsync is encountered, a Task task object is returned in DoSomeAsync, and await passes the method to restore the state machine on the Task object, which is equivalent to calling ContinueWith (). This method, as its name implies, continues with xxx. The thread then returns from the DoSomeAsync. What did you do when you got back? The thread can do something else. At some point in the future, the server sends us a response, and the network driver learns that the request is complete, resumes the method and continues to execute the rest of the code. With a messy picture.
Additional benefits
During garbage cleanup execution in GC, all threads of the application are suspended, and using asynchronous programming means that you can use fewer threads to complete processing with the same amount of concurrency, with the added benefit of fewer threads to clean up. Another thing is that fewer threads are used and fewer CPU thread switches are used.
The above is all the content of the article "sample Analysis of C# Asynchronous programming". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more 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.