從新認識 async/await 語法糖

提起.Net中的 async/await,相信不少.neter 第一反應都會是異步編程,其本質是語法糖,但繼續追查下去,既然是語法糖,那麼通過編譯以後,真正的代碼是什麼樣的,如何執行的?帶着這些疑問,經過網上資料的查詢,能夠了解到編譯以後,是經過實現 IAsyncStateMachine 的一個狀態機來實現的,博客園裏大神Jeffcky 已經說得很清楚了,傳送門: http://www.javashuo.com/article/p-tvpqjkma-gq.htmlhtml

上述知識對咱們理解 async/await 很是重要,但不是本文討論的側重點,觸發筆者寫這篇文章的初衷是:git

1.只有Task能夠被await嗎,await以後就必定是異步執行嗎?

答案固然不是,google了一圈後發現,當一個類能夠被await,必須知足如下條件:github

a. 它必須包含 GetAwaiter() 方法(實例方法或者擴展方法) // 手動劃重點:擴展方法,聰明的你是否是立馬有些思想火花編程

b. GetAwaiter() 返回awatier實例,而且這個實例包含以下條件:異步

  • 必須實現 INotifyCompletion 或者 ICriticalNotifyCompletion 接口
  • 必須包含 IsCompleted 公共屬性
  • 必須包含 GetResult() 方法,返回void或者其餘返回值

上述條件中INotifyCompletion 接口信息以下:async

//
    // 摘要:
    //     Represents an operation that schedules continuations when it completes.
    public interface INotifyCompletion
    {
        //
        // 摘要:
        //     Schedules the continuation action that's invoked when the instance completes.
        //
        // 參數:
        //   continuation:
        //     The action to invoke when the operation completes.
        //
        // 異常:
        //   T:System.ArgumentNullException:
        //     The continuation argument is null (Nothing in Visual Basic).
        void OnCompleted(Action continuation);
    }

重點上述對於參數 continuation 的解釋:委託在操做完成以後調用。此處遺留一個問題:在誰的操做完成以後調用,是怎麼調用的?異步編程

先把上述問題放一邊,咱們來本身寫一個能夠被await的類,而且觀察先後執行的順序以及是否存在線程切換:函數

public class Program {
        static async Task Main (string[] args) {
            Console.WriteLine ($"Begin awati,thread id is {Thread.CurrentThread.ManagedThreadId}");
            int result = await new CustomAwaitable ();
            Console.WriteLine ($"End await,result is {result},thread id is {Thread.CurrentThread.ManagedThreadId}");
            await Task.Delay (Timeout.Infinite);
        }
    }

    public class CustomAwaitable : INotifyCompletion {
        public void OnCompleted (Action continuation) {
            Console.WriteLine ($"Invoke continuation action on completed,thread id is {Thread.CurrentThread.ManagedThreadId}");
            continuation?.Invoke ();
        }

        public int GetResult () {
            Console.WriteLine ($"Get result,thread id is {Thread.CurrentThread.ManagedThreadId}");
            return 100;
        }

        public bool IsCompleted { get; set; }

        public CustomAwaitable GetAwaiter(){
            return this;
        }
    }

上述代碼中,CustomAwaitable 實例知足了可被await的全部條件,而且正常經過編譯,運行後發現結果以下:源碼分析

PS D:\git\awaitable\src> dotnet run
Begin main,thread id is 1
Get awatier,thread id is 1
Begin Invoke continuation action on completed,thread id is 1
Get result,thread id is 1
End main,result is 100,thread id is 1
End Invoke

根據上述日誌,能夠看出:學習

  1. 執行先後線程並未發生切換,因此當咱們不假思索的回答 await/async 就是異步編程時,至少是一個不太嚴謹的答案
  2. 最後執行日誌 "End Invoke" 代表:continuation action 這個委託,根據上述調用日誌順序能夠大體理解爲:編譯器將await以後的代碼封裝爲這個 action,在實例完成後調用OnCompleted方法執行了await 以後的代碼(注:實際狀況比較複雜,若是有多行await,會轉換爲一個狀態機,具體參看文章開頭給出的鏈接)。

2.瞭解了上述知識以後,那麼咱們常規所說的await Task異步編程又是怎麼回事呢?

  1. 先來看Task部分源碼(傳送門):

上述紅框代碼顯示,Task在GetAwaiter中建立了 TaskAwaiter對象,並將this傳遞。

  1. 再來看TaskAwaiter源碼(傳送門):

看到此處,有了前面的知識,咱們會對await task有了更加深刻的理解:
Task經過增長一個GetAwatier()函數,同時將自身傳遞給TaskAwaiter類來實現了await語法糖的支持,同時在執行時,調用GetResult()函數的本質是經過 Task.Wait等待異步線程的執行完成,而後經過回調進行後續的操做。

總結

本文主要對 async/await 語法糖進行分析驗證,同時經過對Task源碼分析,更加深刻的理解此語法糖自己的語法,相信經過經過此文,對你們從多個角度去理解異步編程有幫助,我本身也在不停的學習。

本文代碼示例地址:https://github.com/xBoo/awaitable

相關文章
相關標籤/搜索