C#中異步和多線程的區(qū)別介紹
一、區(qū)別和聯(lián)系
異步和多線程有什么區(qū)別?其實(shí),異步是目的,而多線程是實(shí)現(xiàn)這個(gè)目的的方法。異步是說(shuō),A發(fā)起一個(gè)操作后(一般都是比較耗時(shí)的操作,如果不耗時(shí)的操作就沒(méi)有必要異步了),可以繼續(xù)自顧自的處理它自己的事兒,不用干等著這個(gè)耗時(shí)操作返回。.Net中的這種異步編程模型,就簡(jiǎn)化了多線程編程,我們甚至都不用去關(guān)心Thread類,就可以做一個(gè)異步操作出來(lái)。
異步有的時(shí)候用普通的線程,有的時(shí)候用系統(tǒng)的異步調(diào)用功能。有一些IO操作也是異步的,但是未必需要一個(gè)線程來(lái)運(yùn)行。例如:硬件是有DMA功能的,在調(diào)用DMA傳輸數(shù)據(jù)的時(shí)候,CPU是不需要執(zhí)行處理的,只需要發(fā)起傳輸和等待傳輸結(jié)束即可。具體到.net平臺(tái),比如Socket的BeginSend,如果是運(yùn)行在Windows 2000以后的平臺(tái),在底層就會(huì)調(diào)用異步的完成端口來(lái)發(fā)送。
.Net中的異步執(zhí)行其實(shí)使用的是異步委托。異步委托將要執(zhí)行的方法提交到.net的線程池,由線程池中的線程來(lái)執(zhí)行異步方法。
二、適用范圍
- 當(dāng)需要執(zhí)行I/O操作時(shí),使用異步操作更合適。I/O操作不僅包括了直接的文件、網(wǎng)絡(luò)的讀寫,還包括數(shù)據(jù)庫(kù)操作、Web Service、HttpRequest以及.net Remoting等跨進(jìn)程的調(diào)用。
- 而線程的適用范圍則是那種需要長(zhǎng)時(shí)間CPU運(yùn)算的場(chǎng)合,例如耗時(shí)較長(zhǎng)的圖形處理和算法執(zhí)行。
三、異步的一個(gè)示例
大家可能都知道,使用delegate可以“自動(dòng)”使一個(gè)方法可以進(jìn)行異步的調(diào)用。從直覺(jué)上來(lái)說(shuō),我覺(jué)得是由編譯器或者CLR使用了另外的線程來(lái)執(zhí)行目標(biāo)方法。到底是不是這樣呢??讓我們來(lái)用一段代碼證明一下吧。
delegate void AsyncFoo(int i); static void Main(string[] args) { PrintCurrThreadInfo("Main()"); for (int i = 0; i < 10; i++) { PostAsync(); } Console.ReadLine(); } ///<summary> /// 輸出當(dāng)前線程的信息 ///</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> /// 投遞一個(gè)異步調(diào)用 ///</summary> static void PostAsync() { AsyncFoo caller = new AsyncFoo(Foo); caller.BeginInvoke(1000, new AsyncCallback(FooCallBack), caller); } ///<summary> /// 測(cè)試方法,Sleep一定時(shí)間 ///</summary> ///<param name="i">Sleep的時(shí)間</param> static void Foo(int i) { PrintCurrThreadInfo("Foo()"); Thread.Sleep(i); } 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.
從輸出可以看出,.net 使用 delegate 來(lái)“自動(dòng)”生成的異步調(diào)用是使用了另外的線程(而且是線程池線程)。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C# Winform實(shí)現(xiàn)圓角無(wú)鋸齒按鈕
這篇文章主要為大家詳細(xì)介紹了C# Winform實(shí)現(xiàn)圓角無(wú)鋸齒按鈕,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-07-07C#實(shí)現(xiàn)萬(wàn)物皆可排序的隊(duì)列方法詳解
本文詳細(xì)講解了C#實(shí)現(xiàn)萬(wàn)物皆可排序隊(duì)列的方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-07-07.net4.5使用async和await異步編程實(shí)例
.net4.5使用async和await異步編程實(shí)例,大家參考使用吧2013-12-12C# log4net 日志輸出的實(shí)現(xiàn)示例
本文主要介紹了C# log4net 日志輸出的實(shí)現(xiàn)示例,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-10-10詳解C#如何為某個(gè)方法設(shè)定執(zhí)行超時(shí)時(shí)間
這篇文章主要為大家詳細(xì)介紹一下C#如何為某個(gè)方法設(shè)定執(zhí)行超時(shí)時(shí)間,文中的示例代碼簡(jiǎn)潔易懂,具有一定的借鑒價(jià)值,有需要的小伙伴可以學(xué)習(xí)一下2023-10-10