国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 學(xué)院 > 開(kāi)發(fā)設(shè)計(jì) > 正文

C#多線程與異步的區(qū)別

2019-11-11 04:48:04
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

 隨著擁有多個(gè)硬線程CPU(超線程、雙核)的普及,多線程和異步操作等并發(fā)程序設(shè)計(jì)方法也受到了更多的關(guān)注和討論。本文主要是想與園中各位高手一同探討一下如何使用并發(fā)來(lái)最大化程序的性能。

多線程和異步操作的異同

  多線程和異步操作兩者都可以達(dá)到避免調(diào)用線程阻塞的目的,從而提高軟件的可響應(yīng)性。甚至有些時(shí)候我們就認(rèn)為多線程和異步操作是等同的概念。但是,多線程和異步操作還是有一些區(qū)別的。而這些區(qū)別造成了使用多線程和異步操作的時(shí)機(jī)的區(qū)別。

異步操作的本質(zhì)

  所有的程序最終都會(huì)由計(jì)算機(jī)硬件來(lái)執(zhí)行,所以為了更好的理解異步操作的本質(zhì),我們有必要了解一下它的硬件基礎(chǔ)。 熟悉電腦硬件的朋友肯定對(duì)DMA這個(gè)詞不陌生,硬盤(pán)、光驅(qū)的技術(shù)規(guī)格中都有明確DMA的模式指標(biāo),其實(shí)網(wǎng)卡、聲卡、顯卡也是有DMA功能的。DMA就是直接內(nèi)存訪問(wèn)的意思,也就是說(shuō),擁有DMA功能的硬件在和內(nèi)存進(jìn)行數(shù)據(jù)交換的時(shí)候可以不消耗CPU資源。只要CPU在發(fā)起數(shù)據(jù)傳輸時(shí)發(fā)送一個(gè)指令,硬件就開(kāi)始自己和內(nèi)存交換數(shù)據(jù),在傳輸完成之后硬件會(huì)觸發(fā)一個(gè)中斷來(lái)通知操作完成。這些無(wú)須消耗CPU時(shí)間的I/O操作正是異步操作的硬件基礎(chǔ)。所以即使在DOS這樣的單進(jìn)程(而且無(wú)線程概念)系統(tǒng)中也同樣可以發(fā)起異步的DMA操作。

線程的本質(zhì)

  線程不是一個(gè)計(jì)算機(jī)硬件的功能,而是操作系統(tǒng)提供的一種邏輯功能,線程本質(zhì)上是進(jìn)程中一段并發(fā)運(yùn)行的代碼,所以線程需要操作系統(tǒng)投入CPU資源來(lái)運(yùn)行和調(diào)度。   異步操作的優(yōu)缺點(diǎn)   因?yàn)楫惒讲僮鳠o(wú)須額外的線程負(fù)擔(dān),并且使用回調(diào)的方式進(jìn)行處理,在設(shè)計(jì)良好的情況下,處理函數(shù)可以不必使用共享變量(即使無(wú)法完全不用,最起碼可以減少共享變量的數(shù)量),減少了死鎖的可能。當(dāng)然異步操作也并非完美無(wú)暇。編寫(xiě)異步操作的復(fù)雜程度較高,程序主要使用回調(diào)方式進(jìn)行處理,與普通人的思維方式有些初入,而且難以調(diào)試。

多線程的優(yōu)缺點(diǎn)

  多線程的優(yōu)點(diǎn)很明顯,線程中的處理程序依然是順序執(zhí)行,符合普通人的思維習(xí)慣,所以編程簡(jiǎn)單。但是多線程的缺點(diǎn)也同樣明顯,線程的使用(濫用)會(huì)給系統(tǒng)帶來(lái)上下文切換的額外負(fù)擔(dān)。并且線程間的共享變量可能造成死鎖的出現(xiàn)。

適用范圍

  在了解了線程與異步操作各自的優(yōu)缺點(diǎn)之后,我們可以來(lái)探討一下線程和異步的合理用途。我認(rèn)為:當(dāng)需要執(zhí)行I/O操作時(shí),使用異步操作比使用線程+同步I/O操作更合適。I/O操作不僅包括了直接的文件、網(wǎng)絡(luò)的讀寫(xiě),還包括數(shù)據(jù)庫(kù)操作、Web Service、HttPRequest以及.Net Remoting等跨進(jìn)程的調(diào)用。   而線程的適用范圍則是那種需要長(zhǎng)時(shí)間CPU運(yùn)算的場(chǎng)合,例如耗時(shí)較長(zhǎng)的圖形處理和算法執(zhí)行。但是往往由于使用線程編程的簡(jiǎn)單和符合習(xí)慣,所以很多朋友往往會(huì)使用線程來(lái)執(zhí)行耗時(shí)較長(zhǎng)的I/O操作。這樣在只有少數(shù)幾個(gè)并發(fā)操作的時(shí)候還無(wú)傷大雅,如果需要處理大量的并發(fā)操作時(shí)就不合適了。

實(shí)例研究

  說(shuō)了那么理論上的東西,可能有些兄弟早就不耐煩了,現(xiàn)在我們來(lái)研究幾個(gè)實(shí)際的異步操作例子吧。   實(shí)例1:由delegate產(chǎn)生的異步方法到底是怎么回事?   大家可能都知道,使用delegate可以”自動(dòng)”使一個(gè)方法可以進(jìn)行異步的調(diào)用。從直覺(jué)上來(lái)說(shuō),我覺(jué)得是由編譯器或者CLR使用了另外的線程來(lái)執(zhí)行目標(biāo)方法。到底是不是這樣呢?讓我們來(lái)用一段代碼證明一下吧。   

using System;using System.Threading;namespace AsyncDelegateDemo{ delegate void AsyncFoo(int i); class Program { ///<summary> /// 輸出當(dāng)前線程的信息 ///</summary> ///<param name="name">方法名稱(chēng)</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> /// 測(cè)試方法,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來(lái)”自動(dòng)”生成的異步調(diào)用是使用了另外的線程(而且是線程池線程)。

文獻(xiàn)來(lái)源: http://kb.cnblogs.com/page/116095/


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 股票| 乌拉特后旗| 安远县| 方正县| 博罗县| 长葛市| 松阳县| 大姚县| 洪湖市| 梁平县| 托里县| 南郑县| 永年县| 安吉县| 侯马市| 广东省| 宣恩县| 广河县| 安达市| 宁陕县| 安丘市| 襄樊市| 溧水县| 景东| 驻马店市| 德庆县| 高州市| 榕江县| 通州市| 犍为县| 弋阳县| 崇左市| 靖远县| 乐至县| 柳江县| 新郑市| 康乐县| 吉林省| 高青县| 湖北省| 玛沁县|