第十五節:深入理解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 }
然後在分享服務上的耗時操作,下同。
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 }
下面是案例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結尾的非同步方法)
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
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;
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
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】
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】
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