C#中異步和多線程的區(qū)別介紹
一、區(qū)別和聯(lián)系
異步和多線程有什么區(qū)別?其實,異步是目的,而多線程是實現(xiàn)這個目的的方法。異步是說,A發(fā)起一個操作后(一般都是比較耗時的操作,如果不耗時的操作就沒有必要異步了),可以繼續(xù)自顧自的處理它自己的事兒,不用干等著這個耗時操作返回。.Net中的這種異步編程模型,就簡化了多線程編程,我們甚至都不用去關(guān)心Thread類,就可以做一個異步操作出來。
異步有的時候用普通的線程,有的時候用系統(tǒng)的異步調(diào)用功能。有一些IO操作也是異步的,但是未必需要一個線程來運(yùn)行。例如:硬件是有DMA功能的,在調(diào)用DMA傳輸數(shù)據(jù)的時候,CPU是不需要執(zhí)行處理的,只需要發(fā)起傳輸和等待傳輸結(jié)束即可。具體到.net平臺,比如Socket的BeginSend,如果是運(yùn)行在Windows 2000以后的平臺,在底層就會調(diào)用異步的完成端口來發(fā)送。
.Net中的異步執(zhí)行其實使用的是異步委托。異步委托將要執(zhí)行的方法提交到.net的線程池,由線程池中的線程來執(zhí)行異步方法。
二、適用范圍
- 當(dāng)需要執(zhí)行I/O操作時,使用異步操作更合適。I/O操作不僅包括了直接的文件、網(wǎng)絡(luò)的讀寫,還包括數(shù)據(jù)庫操作、Web Service、HttpRequest以及.net Remoting等跨進(jìn)程的調(diào)用。
- 而線程的適用范圍則是那種需要長時間CPU運(yùn)算的場合,例如耗時較長的圖形處理和算法執(zhí)行。
三、異步的一個示例
大家可能都知道,使用delegate可以“自動”使一個方法可以進(jìn)行異步的調(diào)用。從直覺上來說,我覺得是由編譯器或者CLR使用了另外的線程來執(zhí)行目標(biāo)方法。到底是不是這樣呢??讓我們來用一段代碼證明一下吧。
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>
/// 投遞一個異步調(diào)用
///</summary>
static void PostAsync()
{
AsyncFoo caller = new AsyncFoo(Foo);
caller.BeginInvoke(1000, new AsyncCallback(FooCallBack), caller);
}
///<summary>
/// 測試方法,Sleep一定時間
///</summary>
///<param name="i">Sleep的時間</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 來“自動”生成的異步調(diào)用是使用了另外的線程(而且是線程池線程)。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C# ThreadPool之QueueUserWorkItem使用案例詳解
這篇文章主要介紹了C# ThreadPool之QueueUserWorkItem使用案例詳解,本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-08-08
C#中使用IFormattable實現(xiàn)自定義格式化字符串輸出示例
這篇文章主要介紹了C#中使用IFormattable實現(xiàn)自定義格式字符串輸出示例,本文直接給出實例代碼,需要的朋友可以參考下2015-06-06
C# 指針內(nèi)存控制Marshal內(nèi)存數(shù)據(jù)存儲原理分析
這篇文章主要介紹了C# 指針 內(nèi)存控制 Marshal 內(nèi)存數(shù)據(jù)存儲原理分析,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-02-02
C# winfrom異步加載數(shù)據(jù)不影響窗體UI的操作方法
這篇文章主要介紹了C# winfrom 異步加載數(shù)據(jù)不影響窗體UI,在 WinForms 應(yīng)用程序中,如果數(shù)據(jù)加載是一個比較耗時的操作,直接在主線程中進(jìn)行加載會導(dǎo)致 UI 凍結(jié),這是因為 WinForms 的 UI 是單線程的,主線程被阻塞時就無法處理其他 UI 相關(guān)的任務(wù)2024-12-12
C# 向Word中設(shè)置/更改文本方向的方法(兩種)
在一般情況下word中輸入的文字都是橫向的,今天小編給大家?guī)韮煞N方法來設(shè)置更改文本方向的方法,非常不錯,對c# word 更改文本方向的知識感興趣的朋友一起看看吧2016-08-08

