In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces the knowledge of "how to improve the efficiency of asynchronous execution of Task". Many people will encounter this dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
With the advent of async Task syntax candy, asynchronous programming becomes very simple and suitable for tasks that take a long time.
Some partners may be very confused after using it, and there is almost no difference in time consuming between using async and synchronization.
Let's take a look at an example. In the scenario, multiple third-party WebApi needs to be called to obtain the name, age and gender. Due to the network environment and other reasons, the api response time may be close to 1 second.
Public async Task Test () {var sw = new Stopwatch (); sw.Start (); var userName = await GetUserNameAsync (); var userAge = await GetUserAgeAsync (); var userSex = await GetUserSexAsync (); sw.Stop (); var ts = sw.Elapsed; Console.WriteLine ($"Total time: {ts.TotalMilliseconds} ms");} private async Task GetUserNameAsync () {await Task.Delay (500); return "Xiaoming" } private async Task GetUserAgeAsync () {await Task.Delay (800); return "11";} private async Task GetUserSexAsync () {await Task.Delay (900); return "11";}
After running, it is found that this time is more than 2 seconds, and this user experience must be unbearable.
The reason for this result is that every time an asynchronous call is made, await is added in front of the asynchronous function. For this process alone, it is actually equivalent to synchronization, waiting until the result is returned, each asynchronous function is await, and the time is naturally superimposed. To solve this problem, use a tip to change the code to the following
Public async Task Test () {var sw = new Stopwatch (); sw.Start (); var userNameTask = GetUserNameAsync (); var userAgeTask = GetUserAgeAsync (); var userSexTask = GetUserSexAsync (); var userName = await userNameTask; var userAge = await userAgeTask; var userSex = await userSexTask; sw.Stop (); var ts = sw.Elapsed; Console.WriteLine ($"Total time: {ts.TotalMilliseconds} ms") } private async Task GetUserNameAsync () {await Task.Delay (500); return "Xiaoming";} private async Task GetUserAgeAsync () {await Task.Delay (800); return "11";} private async Task GetUserSexAsync () {await Task.Delay (900); return "11";}
The total time-consuming of this run is the GetUserSexAsync that takes the longest of the three asynchronies.
Why is this so? the key to this trick is here, when the asynchronous function is executed, there is no await, no waiting, these tasks are obediently executed on other threads, and when they are needed, they wait for the return value, so the time will not be superimposed, which is the longest, which will take the most time.
Var userNameTask = GetUserNameAsync (); var userAgeTask = GetUserAgeAsync (); var userSexTask = GetUserSexAsync (); var userName = await userNameTask;var userAge = await userAgeTask;var userSex = await userSexTask; the content of "how to improve the efficiency of asynchronous execution" ends here, thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.