C 多執行緒與非同步的區別詳解

2022-09-26 04:15:10 字數 3552 閱讀 7616

c#多執行緒與非同步的區別詳解

多執行緒和非同步操作的異同

多執行緒和非同步操作兩者都可以達到避免呼叫執行緒阻塞的目的,從而提高軟體的可響應性。甚至有些時候我們就認為多執行緒和非同步操作是等同的概念。但是,多執行緒和非同步操作還是有一些區別的。而這些區別造成了使用多執行緒和非同步操作的時機的區別。

非同步操作的本質

所有的程式最終都會由計算機硬體來執行,所以為了更好的理解非同步操作的本質,我們有必要了解一下它的硬體基礎。 熟悉電腦硬體的朋友肯定對 dma 這個詞不陌生,硬碟、光碟機的技術規格中都有明確 dma 的模式指標,其實網絡卡、音效卡、顯示卡也是有 dma 功能的。dma 就是直接記憶體訪問的意思,也就是說,擁有 dma 功能的硬體在和記憶體進行資料交換的時候可以不消耗 cpu 資源。只要 cpu 在發起資料傳輸時傳送乙個指令,硬體就開始自己和記憶體交換資料,在傳輸完成之後硬體會觸發乙個中斷來通知操作完成。這些無須消耗 cpu 時間的 i/o 操作正是非同步操作的硬體基礎。所以即使在 dos 這樣的單程序(而且無線程概念)系統中也同樣可以發起非同步的 dma 操作。

執行緒的本質

執行緒不是乙個計算機硬體的功能,而是作業系統提供的一種邏輯功能,執行緒本質上是程序中一段併發執行的**,所以執行緒需要作業系統投入 cpu 資源來執行和排程。

非同步操作的優點與缺點

因為非同步操作無須額外的執行緒負擔,並且使用**的方式進行處理,在設計良好的情況下,處理函式可以不必使用共享變數(即使無法完全不用,最起碼可以減少共享變數的數量),減少了死鎖的可能。當然非同步操作也並非完美無暇。編寫非同步操作的複雜程度較高,程式主要使用**方式進行處理,與普通人的思維方式有些初入,而且難以除錯。

多執行緒的優點與缺點

多執行緒的優點很明顯,執行緒中的處理程式依然是順序執行,符合普通人的思維習慣,所以程式設計簡單。但是多執行緒的缺點也同樣明顯,執行緒的使用(濫用)會給系統帶來上下文切換的額外負擔。並且執行緒間的共享變數可能造成死鎖的出現。

適用範圍

在了解了執行緒與非同步操作各自的優點與缺點之後,我們可以來**一下執行緒和非同步的合理用途。我認為:當需要執行 i/o 操作時,使用非同步操作比使用執行緒加同步 i/o 操作更合適。i/o 操作不僅包括了直接的檔案、網路的讀寫,還包括資料庫操作、web service、httprequest 以及 .net remoting 等跨程序的呼叫。

而執行緒的適用範圍則是那種需要長時間 cpu 運算的場合,例如耗時較長的圖形處理和演算法執行。但是往往由於使用執行緒程式設計的簡單和符合習慣,所以很多朋友往往會使用執行緒來執行耗時較長的 i/o 操作。這樣在只有少數幾個併發操作的時候還無傷大雅,如果需要處理大量的併發操作時就不合適了。

例項研究

說了那麼理論上的東西,可能有些兄弟早就不耐煩了,現在我們來研究幾個實際的非同步操作例子吧。

例項1:由 delegate 產生的非同步方法到底是怎麼回事?

大家可能都知道,使用 delegate 可以「自動」使乙個方法可以進行非同步的呼叫。從直覺上來說,我覺得是由編譯器或者 clr 使用了另外的執行緒來執行目標方法。到底是不是這樣呢?讓我們來用一段**證明一下吧。

using system;

using system.threading;

namespace asyncdelegatedemo

////// 測試方法,sleep一定時間

//////sleep的時間

static void foo(int i)

////// 投遞乙個非同步呼叫

///static void postasync()

static void main(string args)

console.readline();

} static void foocallback(iasyncresult 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 threawww.cppcns.comd pool thread.

thread id of foocallback() is: 5, current thread is thread pool thread.

thread id o程式設計客棧f 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.

threa程式設計客棧d 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 來「自動」生成的非同步呼叫是使用了另外的執行緒(而且是執行緒池執行緒)。

本文標題: c#多執行緒與非同步的區別詳解

本文位址: /ruanjian/csharp/192963.html

C 多執行緒與非同步的區別

多執行緒和非同步操作的異同 多執行緒和非同步操作兩者都可以達到避免呼叫執行緒阻塞的目的,從而提高軟體的可響應性。甚至有些時候我們就認為多執行緒和非同步操作是等同的概念。但是,多執行緒和非同步操作還是有一些區別的。而這些區別造成了使用多執行緒和非同步操作的時機的區別。非同步操作的本質 所有的程式最終都...

多執行緒與非同步的區別

多執行緒和非同步操作的異同 多執行緒和非同步操作兩者都可以達到避免呼叫執行緒阻塞的目的,從而提高軟體的可響應性。甚至有些時候我們就認為多執行緒和非同步操作是等同的概念。但是,多執行緒和非同步操作還是有一些區別的。而這些區別造成了使用多執行緒和非同步操作的時機的區別。非同步操作的本質 所有的程式最終都...

同步與非同步多執行緒的區別

先需要講解一下計算機的基本概念 程序 乙個程式在作業系統上執行時,占用作業系統的資源,起名叫程序。執行緒 執行緒是程式執行的最小單位,程序在執行任何乙個操作或者響應,都是由執行緒完成的,執行過程中,使用到作業系統的資源合集,就叫執行緒,程序可以包含多個執行緒或者單單乙個主線程 也稱作ui執行緒 執行...