第十五节:深入理解async和await的作用及各种适用场景和用法(旧,详见最新两篇)

一. 同步VS异步

1.   同步 VS 异步 VS 多线程

同步方法:调用时需要等待返回结果,才可以继续往下执行业务
异步方法:调用时无须等待返回结果,可以继续往下执行业务
开启新线程:在主线程之外开启一个新的线程去执行业务
同步方法和异步方法的本质区别: 调用时是否需要等待返回结果才能继续执行业务

2. 常见的异步方法(都以Async结尾)

  ① HttpClient类:PostAsync、PutAsync、GetAsync、DeleteAsync

  ② EF中DbContext类:SaveChangesAsync

  ③ 文件相关中的:WriteLineAsync

3. 引入异步方法的背景

  比如我在后台要向另一台服务器中获取中的2个接口获取信息,然后将两个接口的信息拼接起来,一起输出,接口1耗时3s,接口2耗时5s,

① 传统的同步方式:

  需要的时间大约为:3s + 5s =8s, 如下面 【案例1】

先分享一个同步请求接口的封装方法,下同。

 1   public class HttpService
 2     {
 3         /// <summary>
 4         /// 后台跨域请求发送代码
 5         /// </summary> 
 6         /// <param name="url">eg:http://ac.guojin.org/jeesite/regist/saveAppAgentAccount </param>
 7         ///<param name="postData"></param>
 8         ///  参数格式(手拼Json) string postData = "{\"name\":\"" + vip.comName + "\",\"shortName\":\"" + vip.shortName + + "\"}";             
 9         /// <returns></returns>
10         public static string PostData(string postData, string url)
11         {
12             HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);//后台请求页面
13             Encoding encoding = Encoding.GetEncoding("utf-8");//注意页面的编码,否则会出现乱码
14             byte[] requestBytes = encoding.GetBytes(postData);
15             req.Method = "POST";
16             req.ContentType = "application/json";
17             req.ContentLength = requestBytes.Length;
18             Stream requestStream = req.GetRequestStream();
19             requestStream.Write(requestBytes, 0, requestBytes.Length);
20             requestStream.Close();
21             HttpWebResponse res = (HttpWebResponse)req.GetResponse();
22             StreamReader sr = new StreamReader(res.GetResponseStream(), System.Text.Encoding.GetEncoding("utf-8"));
23             string backstr = sr.ReadToEnd();//可以读取到从页面返回的结果,以数据流的形式。
24             sr.Close();
25             res.Close();
26 
27             return backstr;
28         }
View Code

然后在分享服务上的耗时操作,下同。

 1  /// <summary>
 2         /// 耗时方法  耗时3s
 3         /// </summary>
 4         /// <returns></returns>
 5         public ActionResult GetMsg1()
 6         {
 7             Thread.Sleep(3000);
 8             return Content("GetMsg1");
 9 
10         }
11 
12         /// <summary>
13         /// 耗时方法  耗时5s
14         /// </summary>
15         /// <returns></returns>
16         public ActionResult GetMsg2()
17         {
18             Thread.Sleep(5000);
19             return Content("GetMsg2");
20 
21         }
View Code

下面是案例1代码

 1        #region 案例1(传统同步方式 耗时8s左右)
 2             {
 3                 Stopwatch watch = Stopwatch.StartNew();
 4                 Console.WriteLine("开始执行");
 5 
 6                 string t1 = HttpService.PostData("", "http://localhost:2788/Home/GetMsg1");
 7                 string t2 = HttpService.PostData("", "http://localhost:2788/Home/GetMsg2");
 8 
 9                 Console.WriteLine("我是主业务");
10                 Console.WriteLine($"{t1},{t2}");
11                 watch.Stop();
12                 Console.WriteLine($"耗时:{watch.ElapsedMilliseconds}");
13             }
14             #endregion

② 开启新线程分别执行两个耗时操作

  需要的时间大约为:Max(3s,5s) = 5s ,如下面【案例2】

 1         #region 案例2(开启新线程分别执行两个耗时操作 耗时5s左右)
 2             {
 3                 Stopwatch watch = Stopwatch.StartNew();
 4                 Console.WriteLine("开始执行");
 5 
 6                 var task1 = Task.Run(() =>
 7                 {
 8                     return HttpService.PostData("", "http://localhost:2788/Home/GetMsg1");
 9                 });
10 
11                 var task2 = Task.Run(() =>
12                 {
13                     return HttpService.PostData("", "http://localhost:2788/Home/GetMsg2");
14                 });
15 
16                 Console.WriteLine("我是主业务");
17                 //主线程进行等待
18                 Task.WaitAll(task1, task2);
19                 Console.WriteLine($"{task1.Result},{task2.Result}");
20                 watch.Stop();
21                 Console.WriteLine($"耗时:{watch.ElapsedMilliseconds}");
22             }
23             #endregion

  既然②方式可以解决同步方法串行耗时间的问题,但这种方式存在一个弊端,一个业务中存在多个线程,且需要对线程进行管理,相对麻烦,从而引出了异步方法。

这里的异步方法 我 特指:系统类库自带的以async结尾的异步方法。

③ 使用系统类库自带的异步方法

  需要的时间大约为:Max(3s,5s) = 5s ,如下面【案例3】

 1       #region 案例3(使用系统类库自带的异步方法 耗时5s左右)
 2             {
 3                 Stopwatch watch = Stopwatch.StartNew();
 4                 HttpClient http = new HttpClient();
 5                 var httpContent = new StringContent("", Encoding.UTF8, "application/json");
 6                 Console.WriteLine("开始执行");
 7                 //执行业务
 8                 var r1 = http.PostAsync("http://localhost:2788/Home/GetMsg1", httpContent);
 9                 var r2 = http.PostAsync("http://localhost:2788/Home/GetMsg2", httpContent);
10                 Console.WriteLine("我是主业务");
11 
12                 //通过异步方法的结果.Result可以是异步方法执行完的结果
13                 Console.WriteLine(r1.Result.Content.ReadAsStringAsync().Result);
14                 Console.WriteLine(r2.Result.Content.ReadAsStringAsync().Result);
15 
16                 watch.Stop();
17                 Console.WriteLine($"耗时:{watch.ElapsedMilliseconds}");
18             }
19             #endregion

PS:通过 .Result 来获取异步方法执行完后的结果。

二. 利用async和await封装异步方法

1. 首先要声明几点:

  ① async和await关键字是C# 5.0时代引入的,它是一种异步编程模型

  ② 它们本身并不创建新线程,但我可以在自行封装的async中利用Task.Run开启新线程

  ③ 利用async关键字封装的方法中如果写全部都是一些串行业务, 且不用await关键字,那么即使使用async封装,也并没有什么卵用,并起不了异步方法的作用。

   需要的时间大约为:3s + 5s =8s, 如下面 【案例4】,并且封装的方法编译器会提示:“缺少关键字await,将以同步的方式调用,请使用await运算符等待非阻止API或Task.Run的形式”(PS:非阻止API指系统类库自带的以Async结尾的异步方法)

 1        //利用async封装同步业务的方法
 2         private static async Task<string> NewMethod5Async()
 3         {
 4             Thread.Sleep(3000);
 5             //其它同步业务
 6             return "Msg1";
 7         }
 8         private static async Task<string> NewMethod6Async()
 9         {
10             Thread.Sleep(5000);
11             //其它同步业务
12             return "Msg2";
13         }
View Code
 1            #region 案例4(async关键字封装的方法中如果写全部都是一些串行业务 耗时8s左右)
 2             {
 3                 Stopwatch watch = Stopwatch.StartNew();
 4 
 5                 Console.WriteLine("开始执行");
 6 
 7                 Task<string> t1 = NewMethod5Async();
 8                 Task<string> t2 = NewMethod6Async();
 9 
10                 Console.WriteLine("我是主业务");
11                 Console.WriteLine($"{t1.Result},{t2.Result}");
12                 watch.Stop();
13                 Console.WriteLine($"耗时:{watch.ElapsedMilliseconds}");
14             }
15             #endregion

  观点结论1:从上面③中可以得出一个结论,async中必须要有await运算符才能起到异步方法的作用,且await 运算符只能加在 系统类库默认提供的异步方法或者新线程(如:Task.Run)前面。

   如:下面【案例5】【案例6】需要的时间大约为:Max(3s,5s) = 5s

 1 // 将系统类库提供的异步方法利用async封装起来
 2         private static async Task<String> NewMethod1Async()
 3         {
 4             HttpClient http = new HttpClient();
 5             var httpContent = new StringContent("", Encoding.UTF8, "application/json");
 6             //执行业务
 7             var r1 = await http.PostAsync("http://localhost:2788/Home/GetMsg1", httpContent);
 8             return r1.Content.ReadAsStringAsync().Result;
 9         }
10         private static async Task<String> NewMethod2Async()
11         {
12             HttpClient http = new HttpClient();
13             var httpContent = new StringContent("", Encoding.UTF8, "application/json");
14             //执行业务
15             var r1 = await http.PostAsync("http://localhost:2788/Home/GetMsg2", httpContent);
16             return r1.Content.ReadAsStringAsync().Result;
17         }
18 
19         //将await关键字加在新线程的前面
20         private static async Task<string> NewMethod3Async()
21         {
22             var msg = await Task.Run(() =>
23             {
24                 return HttpService.PostData("", "http://localhost:2788/Home/GetMsg1");
25             });
26             return msg;
27         }
28         private static async Task<string> NewMethod4Async()
29         {
30             var msg = await Task.Run(() =>
31             {
32                 return HttpService.PostData("", "http://localhost:2788/Home/GetMsg2");
33             });
34             return msg;
35         }
View Code
 1        #region 案例5(将系统类库提供的异步方法利用async封装起来 耗时5s左右)
 2             //并且先输出“我是主业务”,证明t1和t2是并行执行的,且不阻碍主业务
 3             {
 4                 Stopwatch watch = Stopwatch.StartNew();
 5 
 6                 Console.WriteLine("开始执行");
 7                 Task<string> t1 = NewMethod1Async();
 8                 Task<string> t2 = NewMethod2Async();
 9 
10                 Console.WriteLine("我是主业务");
11                 Console.WriteLine($"{t1.Result},{t2.Result}");
12                 watch.Stop();
13                 Console.WriteLine($"耗时:{watch.ElapsedMilliseconds}");
14             }
15             #endregion

 1        #region 案例6(将新线程利用async封装起来 耗时5s左右)
 2             //并且先输出“我是主业务”,证明t1和t2是并行执行的,且不阻碍主业务
 3             {
 4                 Stopwatch watch = Stopwatch.StartNew();
 5 
 6                 Console.WriteLine("开始执行");
 7                 Task<string> t1 = NewMethod3Async();
 8                 Task<string> t2 = NewMethod4Async();
 9 
10                 Console.WriteLine("我是主业务");
11                 Console.WriteLine($"{t1.Result},{t2.Result}");
12                 watch.Stop();
13                 Console.WriteLine($"耗时:{watch.ElapsedMilliseconds}");
14             }
15             #endregion

2. 几个规则和约定

  ① async封装的方法中,可以有多个await,这里的await代表等待该行代码执行完毕。

  ② 我们通常自己封装的方法也要以Async结尾,方便识别

  ③ 异步返回类型主要有三种:Task<T> 、Task、Void

3. 测试得出其他几个结论

① 如果async封装的异步方法里既有同步业务又有异步业务(开启新线程或者系统类库提供异步方法),那么同步方法那部分的时间在调用的时候是会阻塞主线程的,即主线程要等待这部分同步业务执行完才能往下执行。

  如【案例7】 耗时:同步操作之和 2s+2s + Max(3s,5s)=9s;

 1   //同步耗时操作和异步方法同时封装
 2         private static async Task<String> NewMethod7Async()
 3         {
 4             //调用异步方法之前还有一个耗时操作
 5             Thread.Sleep(2000);
 6 
 7             //下面的操作耗时3s
 8             HttpClient http = new HttpClient();
 9             var httpContent = new StringContent("", Encoding.UTF8, "application/json");
10             //执行业务
11             var r1 = await http.PostAsync("http://localhost:2788/Home/GetMsg1", httpContent);
12             return r1.Content.ReadAsStringAsync().Result;
13         }
14         private static async Task<String> NewMethod8Async()
15         {
16             //调用异步方法之前还有一个耗时操作
17             Thread.Sleep(2000);
18 
19             //下面的操作耗时5s
20             HttpClient http = new HttpClient();
21             var httpContent = new StringContent("", Encoding.UTF8, "application/json");
22             //执行业务
23             var r1 = await http.PostAsync("http://localhost:2788/Home/GetMsg2", httpContent);
24             return r1.Content.ReadAsStringAsync().Result;
25         }
View Code
 1       #region 案例7(既有普通的耗时操作,也有系统本身的异步方法,耗时9s左右)
 2             //且大约4s后才能输出 “我是主业务”,证明同步操作Thread.Sleep(2000);  阻塞主线程
 3             {
 4                 Stopwatch watch = Stopwatch.StartNew();
 5 
 6                 Console.WriteLine("开始执行");
 7                 Task<string> t1 = NewMethod7Async();
 8                 Task<string> t2 = NewMethod8Async();
 9 
10                 Console.WriteLine("我是主业务");
11                 Console.WriteLine($"{t1.Result},{t2.Result}");
12                 watch.Stop();
13                 Console.WriteLine($"耗时:{watch.ElapsedMilliseconds}");
14             }
15             #endregion

  

  证明:async封装的异步方法里的同步业务的时间会阻塞主线程,再次证明 await只能加在 非阻止api和开启新线程的前面

② 如果封装的异步方法中存在等待的问题,而且不能阻塞主线程(不能用Thread.Sleep) , 这个时候可以用Task.Delay,并在前面加await关键字

  如【案例8】 耗时:Max(2+3 , 5+2)=7s

 1    //利用Task.Delay(2000);等待
 2         private static async Task<String> NewMethod11Async()
 3         {
 4             //调用异步方法之前需要等待2s
 5             await Task.Delay(2000);
 6 
 7             //下面的操作耗时3s
 8             HttpClient http = new HttpClient();
 9             var httpContent = new StringContent("", Encoding.UTF8, "application/json");
10             //执行业务
11             var r1 = await http.PostAsync("http://localhost:2788/Home/GetMsg1", httpContent);
12             return r1.Content.ReadAsStringAsync().Result;
13         }
14 
15         private static async Task<String> NewMethod12Async()
16         {
17             //调用异步方法之前需要等待2s
18             await Task.Delay(2000);
19 
20             //下面的操作耗时5s
21             HttpClient http = new HttpClient();
22             var httpContent = new StringContent("", Encoding.UTF8, "application/json");
23             //执行业务
24             var r1 = await http.PostAsync("http://localhost:2788/Home/GetMsg2", httpContent);
25             return r1.Content.ReadAsStringAsync().Result;
26         }
View Code
 1         #region 案例8(利用Task.Delay执行异步方法的等待操作)
 2             //结果是7s,且马上输出“我是主业务”,说明Task.Delay(),不阻塞主线程。
 3             {
 4                 Stopwatch watch = Stopwatch.StartNew();
 5                 Console.WriteLine("开始执行");
 6                 Task<string> t1 = NewMethod11Async();
 7                 Task<string> t2 = NewMethod12Async();
 8 
 9                 Console.WriteLine("我是主业务");
10                 Console.WriteLine($"{t1.Result},{t2.Result}");
11                 watch.Stop();
12                 Console.WriteLine($"耗时:{watch.ElapsedMilliseconds}");
13             }
14             #endregion

三. 异步方法返回类型

1. Task<T>, 处理含有返回值的异步方法,通过 .Result 等待异步方法执行完,且获取到返回值。

2. Task:调用方法不需要从异步方法中取返回值,但是希望检查异步方法的状态,那么可以选择可以返回 Task 类型的对象。不过,就算异步方法中包含 return 语句,也不会返回任何东西。

  如【案例9】

 1   
 2         //返回值为Task的方法
 3         private static async Task NewMethod9Async()
 4         {
 5 
 6             //下面的操作耗时3s
 7             HttpClient http = new HttpClient();
 8             var httpContent = new StringContent("", Encoding.UTF8, "application/json");
 9             //执行业务
10             var r1 = await http.PostAsync("http://localhost:2788/Home/GetMsg1", httpContent);
11             Console.WriteLine("NewMethod9Async执行完成");
12         }
View Code
 1        #region 案例9(返回值为Task的异步方法)
 2             //结果是5s,说明异步方法和主线程的同步方法 在并行执行
 3             {
 4                 Stopwatch watch = Stopwatch.StartNew();
 5 
 6                 Console.WriteLine("开始执行");
 7                 Task t = NewMethod9Async();
 8 
 9                 Console.WriteLine($"{nameof(t.Status)}: {t.Status}");   //任务状态
10                 Console.WriteLine($"{nameof(t.IsCompleted)}: {t.IsCompleted}");     //任务完成状态标识
11                 Console.WriteLine($"{nameof(t.IsFaulted)}: {t.IsFaulted}");     //任务是否有未处理的异常标识
12 
13                 //执行其他耗时操作,与此同时NewMethod9Async也在工作
14                 Thread.Sleep(5000);
15      
16                 Console.WriteLine("我是主业务");
17 
18                 t.Wait();
19 
20                 Console.WriteLine($"{nameof(t.Status)}: {t.Status}");   //任务状态
21                 Console.WriteLine($"{nameof(t.IsCompleted)}: {t.IsCompleted}");     //任务完成状态标识
22                 Console.WriteLine($"{nameof(t.IsFaulted)}: {t.IsFaulted}");     //任务是否有未处理的异常标识
23 
24                 Console.WriteLine($"所有业务执行完成了");
25                 watch.Stop();
26                 Console.WriteLine($"耗时:{watch.ElapsedMilliseconds}");
27             }
28             #endregion

  PS:对于Task返回值的异步方法,可以调用Wait(),等 待该异步方法执行完,他和await不同,await必须出现在async关键字封装的方法中。

3. void:调用异步执行方法,不需要做任何交互

  如【案例10】

 1     //返回值是Void的方法
 2         private static async void NewMethod10Async()
 3         {
 4             //下面的操作耗时5s
 5             HttpClient http = new HttpClient();
 6             var httpContent = new StringContent("", Encoding.UTF8, "application/json");
 7             //执行业务,假设这里主需要请求,不需要做任何交互
 8             var r1 = await http.PostAsync("http://localhost:2788/Home/GetMsg1", httpContent);
 9             Console.WriteLine("NewMethod10Async执行完成");
10         }
View Code
 1         #region 案例10(返回值为Void的异步方法)
 2             //结果是5s,说明异步方法和主线程的同步方法 在并行执行
 3             {
 4                 Stopwatch watch = Stopwatch.StartNew();
 5 
 6                 Console.WriteLine("开始执行");
 7                 NewMethod10Async();
 8 
 9                 //执行其他耗时操作,与此同时NewMethod9Async也在工作
10                 Thread.Sleep(5000);
11 
12                 Console.WriteLine("我是主业务");
13 
14 
15                 Console.WriteLine($"所有业务执行完成了");
16                 watch.Stop();
17                 Console.WriteLine($"耗时:{watch.ElapsedMilliseconds}");
18             }
19             #endregion

四. 几个结论

1. 异步方法到底开不开起新线程?

  异步和等待关键字不会导致其他线程创建。 因为异步方法本身并不会运行的线程,异步方法不需要多线程。 只有 + 当方法处于活动状态,则方法在当前同步上下文中运行并使用在线程的时间。 可以使用 Task.Run 移动 CPU 工作移到后台线程,但是,后台线程不利于等待结果变得可用处理。(来自MSDN原话)

2. async和await是一种异步编程模型,它本身并不能开启新线程,多用于将一些非阻止API或者开启新线程的操作封装起来,使其调用的时候像同步方法一样使用。

下面补充博客园dudu的解释,方便大家理解。

 

五. 参考资料

   1. 反骨仔:http://www.cnblogs.com/liqingwen/p/5831951.html

        http://www.cnblogs.com/liqingwen/p/5844095.html

  2. MSDN:https://msdn.microsoft.com/library/hh191443(vs.110).aspx

 

PS:如果你想了解多线程的其他知识,请移步:那些年我们一起追逐的多线程(Thread、ThreadPool、委托异步调用、Task/TaskFactory、Parallerl、async和await)

 

 

 

 

 

 

!

  • 作       者 : Yaopengfei(姚鹏飞)
  • 博客地址 : http://www.cnblogs.com/yaopengfei/
  • 声     明1 : 本人才疏学浅,用郭德纲的话说“我是一个小学生”,如有错误,欢迎讨论,请勿谩骂^_^。
  • 声     明2 : 原创博客请在转载时保留原文链接或在文章开头加上本人博客地址,如需代码请加我QQ:604649488 (备注:评论的博客名)
 
posted @ 2018-07-02 07:53  Yaopengfei  阅读(31056)  评论(49编辑  收藏  举报