In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
How to carry out Task.CompletedTask and Task.Result analysis, I believe that many inexperienced people do not know what to do. Therefore, this paper summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.
In any method that returns Task, it is best to avoid using Task.Run if the result can be calculated without asynchronism. For example, a short calculation function, or a pre-calculated result returned in a test, does not need to use Task.Run.
For example, an interface method that returns Task is defined, but there is no particularly time-consuming code in the implementation.
Public interface IComputer {Task Do (); Task DoString ();} is usually implemented like this. Even if the code in the implementation is short. Public class Computer: IComputer {public Task Do () {return Task.Run (() = > {/ / logic code});} public Task DoString () {return Task.Run (() = > {/ / logic code return "aaa";}) }} in fact, there are two helpers on the Task class that make the code more readable and require less runtime overhead.
I prefer to use Task.CompletedTask for the Do method in the above interface implementation.
Public Task Do () {/ / Logic Code return Task.CompletedTask;}
Let's take a look at the definition of CompletedTask
/ Gets a task that has already completed successfully./// The successfully completed task.public static Task CompletedTask {get;}
What if I return Task? In this case, if you already have the answer, use Task.FromResult to wrap the answer.
Public Task DoString () {/ / logical code return Task.FromResult ("aaa");}
Take a look at the definition of FromResult
/ Creates a that's completed successfully with the specified result./// The result to store into the completed task./// The type of the result returned by the task./// The successfully completed task.public static Task FromResult (TResult result)
Through the above changes, it not only realizes the implementation of the interface, but also reduces the system overhead of Task.Run calls.
After reading the above, have you mastered the method of Task.CompletedTask and Task.Result analysis? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!
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.