C#多線程與異步的區(qū)別詳解
C#多線程與異步的區(qū)別詳解
隨著擁有多個(gè)硬線程 CPU(超線程、雙核)的普及,多線程和異步操作等并發(fā)程序設(shè)計(jì)方法也受到了更多的關(guān)注和討論。本文主要是想與各位高手一同探討一下如何使用并發(fā)來最大化程序的性能。
多線程和異步操作的異同
多線程和異步操作兩者都可以達(dá)到避免調(diào)用線程阻塞的目的,從而提高軟件的可響應(yīng)性。甚至有些時(shí)候我們就認(rèn)為多線程和異步操作是等同的概念。但是,多線程和異步操作還是有一些區(qū)別的。而這些區(qū)別造成了使用多線程和異步操作的時(shí)機(jī)的區(qū)別。
異步操作的本質(zhì)
所有的程序最終都會由計(jì)算機(jī)硬件來執(zhí)行,所以為了更好的理解異步操作的本質(zhì),我們有必要了解一下它的硬件基礎(chǔ)。 熟悉電腦硬件的朋友肯定對 DMA 這個(gè)詞不陌生,硬盤、光驅(qū)的技術(shù)規(guī)格中都有明確 DMA 的模式指標(biāo),其實(shí)網(wǎng)卡、聲卡、顯卡也是有 DMA 功能的。DMA 就是直接內(nèi)存訪問的意思,也就是說,擁有 DMA 功能的硬件在和內(nèi)存進(jìn)行數(shù)據(jù)交換的時(shí)候可以不消耗 CPU 資源。只要 CPU 在發(fā)起數(shù)據(jù)傳輸時(shí)發(fā)送一個(gè)指令,硬件就開始自己和內(nèi)存交換數(shù)據(jù),在傳輸完成之后硬件會觸發(fā)一個(gè)中斷來通知操作完成。這些無須消耗 CPU 時(shí)間的 I/O 操作正是異步操作的硬件基礎(chǔ)。所以即使在 DOS 這樣的單進(jìn)程(而且無線程概念)系統(tǒng)中也同樣可以發(fā)起異步的 DMA 操作。
線程的本質(zhì)
線程不是一個(gè)計(jì)算機(jī)硬件的功能,而是操作系統(tǒng)提供的一種邏輯功能,線程本質(zhì)上是進(jìn)程中一段并發(fā)運(yùn)行的代碼,所以線程需要操作系統(tǒng)投入 CPU 資源來運(yùn)行和調(diào)度。
異步操作的優(yōu)點(diǎn)與缺點(diǎn)
因?yàn)楫惒讲僮鳠o須額外的線程負(fù)擔(dān),并且使用回調(diào)的方式進(jìn)行處理,在設(shè)計(jì)良好的情況下,處理函數(shù)可以不必使用共享變量(即使無法完全不用,最起碼可以減少共享變量的數(shù)量),減少了死鎖的可能。當(dāng)然異步操作也并非完美無暇。編寫異步操作的復(fù)雜程度較高,程序主要使用回調(diào)方式進(jìn)行處理,與普通人的思維方式有些初入,而且難以調(diào)試。
多線程的優(yōu)點(diǎn)與缺點(diǎn)
多線程的優(yōu)點(diǎn)很明顯,線程中的處理程序依然是順序執(zhí)行,符合普通人的思維習(xí)慣,所以編程簡單。但是多線程的缺點(diǎn)也同樣明顯,線程的使用(濫用)會給系統(tǒng)帶來上下文切換的額外負(fù)擔(dān)。并且線程間的共享變量可能造成死鎖的出現(xiàn)。
適用范圍
在了解了線程與異步操作各自的優(yōu)點(diǎn)與缺點(diǎn)之后,我們可以來探討一下線程和異步的合理用途。我認(rèn)為:當(dāng)需要執(zhí)行 I/O 操作時(shí),使用異步操作比使用線程加同步 I/O 操作更合適。I/O 操作不僅包括了直接的文件、網(wǎng)絡(luò)的讀寫,還包括數(shù)據(jù)庫操作、Web Service、HttpRequest 以及 .Net Remoting 等跨進(jìn)程的調(diào)用。
而線程的適用范圍則是那種需要長時(shí)間 CPU 運(yùn)算的場合,例如耗時(shí)較長的圖形處理和算法執(zhí)行。但是往往由于使用線程編程的簡單和符合習(xí)慣,所以很多朋友往往會使用線程來執(zhí)行耗時(shí)較長的 I/O 操作。這樣在只有少數(shù)幾個(gè)并發(fā)操作的時(shí)候還無傷大雅,如果需要處理大量的并發(fā)操作時(shí)就不合適了。
實(shí)例研究
說了那么理論上的東西,可能有些兄弟早就不耐煩了,現(xiàn)在我們來研究幾個(gè)實(shí)際的異步操作例子吧。
實(shí)例1:由 delegate 產(chǎn)生的異步方法到底是怎么回事?
大家可能都知道,使用 delegate 可以“自動”使一個(gè)方法可以進(jìn)行異步的調(diào)用。從直覺上來說,我覺得是由編譯器或者 CLR 使用了另外的線程來執(zhí)行目標(biāo)方法。到底是不是這樣呢?讓我們來用一段代碼證明一下吧。
using System; using System.Threading; namespace AsyncDelegateDemo { delegate void AsyncFoo(int i); class Program { ///<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> /// 測試方法,Sleep一定時(shí)間 ///</summary> ///<param name="i">Sleep的時(shí)間</param> static void Foo(int i) { PrintCurrThreadInfo("Foo()"); Thread.Sleep(i); } ///<summary> /// 投遞一個(gè)異步調(diào)用 ///</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.
從輸出可以看出,.net 使用 delegate 來“自動”生成的異步調(diào)用是使用了另外的線程(而且是線程池線程)。
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
欄 目:C#教程
下一篇:C# WindowsForm程序同時(shí)啟動多個(gè)窗口類
本文標(biāo)題:C#多線程與異步的區(qū)別詳解
本文地址:http://mengdiqiu.com.cn/a1/C_jiaocheng/5686.html
您可能感興趣的文章
- 01-10C#停止線程的方法
- 01-10C#通過重寫Panel改變邊框顏色與寬度的方法
- 01-10C#實(shí)現(xiàn)多線程下載文件的方法
- 01-10C#實(shí)現(xiàn)實(shí)體類與字符串互相轉(zhuǎn)換的方法
- 01-10C#實(shí)現(xiàn)多線程寫入同一個(gè)文件的方法
- 01-10C#獲取進(jìn)程或線程相關(guān)信息的方法
- 01-10C#通過Semaphore類控制線程隊(duì)列的方法
- 01-10C#線程隊(duì)列用法實(shí)例分析
- 01-10C#實(shí)現(xiàn)子窗體與父窗體通信方法實(shí)例總結(jié)
- 01-10時(shí)間戳與時(shí)間相互轉(zhuǎn)換(php .net精確到毫秒)


閱讀排行
本欄相關(guān)
- 01-10C#通過反射獲取當(dāng)前工程中所有窗體并
- 01-10關(guān)于ASP網(wǎng)頁無法打開的解決方案
- 01-10WinForm限制窗體不能移到屏幕外的方法
- 01-10WinForm繪制圓角的方法
- 01-10C#實(shí)現(xiàn)txt定位指定行完整實(shí)例
- 01-10WinForm實(shí)現(xiàn)仿視頻播放器左下角滾動新
- 01-10C#停止線程的方法
- 01-10C#實(shí)現(xiàn)清空回收站的方法
- 01-10C#通過重寫Panel改變邊框顏色與寬度的
- 01-10C#實(shí)現(xiàn)讀取注冊表監(jiān)控當(dāng)前操作系統(tǒng)已
隨機(jī)閱讀
- 04-02jquery與jsp,用jquery
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 08-05織夢dedecms什么時(shí)候用欄目交叉功能?
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 01-11ajax實(shí)現(xiàn)頁面的局部加載
- 01-10delphi制作wav文件的方法
- 01-10使用C語言求解撲克牌的順子及n個(gè)骰子
- 01-10C#中split用法實(shí)例總結(jié)