標籤:
非同步作業的本質
在方法調用前為非同步方法呼叫指定一個回呼函數,方法調用後被線程池中的一個線程接管,執行該方法。主線程立即返回,繼續執行其他工作或響應使用者請求。如果非同步方法呼叫執行完 畢,回呼函數被自動執行,以處理非同步方法呼叫的調用結果。 如何?非同步方法呼叫呢?C#通過非同步委託調用BeginInvoke和EndInvoke方法來實現非同步方法呼叫。
BeginInvoke方法原型: IAsyncResult BeginInvoke(......, AsyncCallback callback, object o); ......表示非同步委託中定義的參數列表。 AsyncCallback參數是一個用於 回呼函數的委託,它的原型為: public delegate void AsyncCallback(IAsyncResult ar)。其中IAsyncResult參數用於封裝非同步方法呼叫的執行結果。 Object參數用於在主線程與回呼函數間傳遞一些附加資訊,如同步資訊。
EndInvoke方法原型: xxx EndInvoke(IAsyncResult result); xxx表示非同步委託原型中定義的返回資料類型,IAsyncResult用於封裝非同步方法呼叫的執行結果。
線程的本質
線程不是一個電腦硬體的功能,而是作業系統提供的一種邏輯功能,線程本質上是進程中一段並發啟動並執行代碼,所以線程需要作業系統投入CPU資源來運行和調度。
非同步作業的優缺點
因為非同步作業無須額外的線程負擔,並且使用回調的方式進行處理,在設計良好的情況下,處理函數可以不必使用共用變數(即使無法完全不用,最起碼可以減少共用變數的數量),減少了死結的可能。當然非同步作業也並非完美無暇。編寫非同步作業的複雜程度較高,程式主要使用回調方式進行處理,與普通人的思維方式有些初入,而且難以調試。
多線程的優缺點
多線程的優點很明顯,線程中的處理常式依然是順序執行,符合普通人的思維習慣,所以編程簡單。但是多線程的缺點也同樣明顯,線程的使用(濫用)會給系統帶來環境切換的額外負擔。並且線程間的共用變數可能造成死結的出現。
適用範圍
在瞭解了線程與非同步作業各自的優缺點之後,我們可以來探討一下線程和非同步合理用途。我認為:當需要執行I/O操作時,使用非同步作業比使用線程+同步I/O操作更合適。I/O操作不僅包括了直接的檔案、網路的讀寫,還包括資料庫操作、Web Service、HttpRequest以及.Net Remoting等跨進程的調用。
而線程的適用範圍則是那種需要長時間CPU運算的場合,例如耗時較長的圖形處理和演算法執行。但是往往由於使用線程編程的簡單和符合習慣,所以很多朋友往往會使用線程來執行耗時較長的I/O操作。這樣在只有少數幾個並行作業的時候還無傷大雅,如果需要處理大量的並行作業時就不合適了。
下面看個非同步呼叫的執行個體:
using System;using System.Threading;namespace AsyncDelegateDemo{ delegate void AsyncFoo(int i); class Program { ///<summary> /// 輸出當前線程的資訊 ///</summary> ///<param name="name">方法名稱</param> static void PrintCurrThreadInfo(string name) { Console.WriteLine("Thread Id of " + name+ " is: " + Thread.CurrentThread.ManagedThreadId+ ", current thread is " + (Thread.CurrentThread.IsThreadPoolThread ? "" : "not ") + "thread pool thread."); } ///<summary> /// 測試方法,Sleep一定時間 ///</summary> ///<param name="i">Sleep的時間</param> static void Foo(int i) { PrintCurrThreadInfo("Foo()"); Thread.Sleep(i); } ///<summary> /// 投遞一個非同步呼叫 ///</summary> static void PostAsync() { AsyncFoo caller = new AsyncFoo(Foo); caller.BeginInvoke(1000, new AsyncCallback(FooCallBack), caller); } static void Main(string[] args) { PrintCurrThreadInfo("Main()"); for(int i = 0; i < 10 ; i++) { PostAsync(); } Console.ReadLine(); } static void FooCallBack(IAsyncResult ar) { PrintCurrThreadInfo("FooCallBack()"); AsyncFoo caller = (AsyncFoo) ar.AsyncState; caller.EndInvoke(ar); } }}
這段代碼代碼的輸出如下:
Thread Id of Main() is: 1, current thread is not thread pool thread.Thread Id of Foo() is: 3, current thread is thread pool thread.Thread Id of FooCallBack() is: 3, current thread is thread pool thread.Thread Id of Foo() is: 3, current thread is thread pool thread.Thread Id of Foo() is: 4, current thread is thread pool thread.Thread Id of Foo() is: 5, current thread is thread pool thread.Thread Id of FooCallBack() is: 3, current thread is thread pool thread.Thread Id of Foo() is: 3, current thread is thread pool thread.Thread Id of FooCallBack() is: 4, current thread is thread pool thread.Thread Id of Foo() is: 4, current thread is thread pool thread.Thread Id of Foo() is: 6, current thread is thread pool thread.Thread Id of FooCallBack() is: 5, current thread is thread pool thread.Thread Id of Foo() is: 5, current thread is thread pool thread.Thread Id of Foo() is: 7, current thread is thread pool thread.Thread Id of FooCallBack() is: 3, current thread is thread pool thread.Thread Id of Foo() is: 3, current thread is thread pool thread.Thread Id of FooCallBack() is: 4, current thread is thread pool thread.Thread Id of FooCallBack() is: 6, current thread is thread pool thread.Thread Id of FooCallBack() is: 5, current thread is thread pool thread.Thread Id of FooCallBack() is: 7, current thread is thread pool thread.Thread Id of FooCallBack() is: 3, current thread is thread pool thread.
c# 多線程與非同步呼叫