C#線程處理系列之線程池中的I/O線程
一、I/O線程實(shí)現(xiàn)對文件的異步
1.1 I/O線程介紹:
對于線程所執(zhí)行的任務(wù)來說,可以把線程分為兩種類型:工作者線程和I/O線程。
工作者線程用來完成一些計(jì)算的任務(wù),在任務(wù)執(zhí)行的過程中,需要CPU不間斷地處理,所以,在工作者線程的執(zhí)行過程中,CPU和線程的資源是充分利用的。
I/O線程主要用來完成輸入和輸出的工作的,在這種情況下, 計(jì)算機(jī)需要I/O設(shè)備完成輸入和輸出的任務(wù),在處理過程中,CPU是不需要參與處理過程的,此時(shí)正在運(yùn)行的線程將處于等待狀態(tài),只有等任務(wù)完成后才會有事可做, 這樣就造成線程資源浪費(fèi)的問題。為了解決這樣的問題,可以通過線程池來解決這樣的問題,讓線程池來管理線程,前面已經(jīng)介紹過線程池了, 在這里就不講了。
對于I/O線程,我們可以將輸入輸出操作分成三個(gè)步驟:啟動(dòng)、實(shí)際輸入輸出、處理結(jié)果。用于實(shí)際輸入輸出可由硬件完成,并不需要CPU的參與,而啟動(dòng)和處理結(jié)果也可以不在同一個(gè)線程上,這樣就可以充分利用線程資源。在.Net中通過以Begin開頭的方法來完成啟動(dòng),以End開頭的方法來處理結(jié)果,這兩個(gè)方法可以運(yùn)行在不同的線程,這樣我們就實(shí)現(xiàn)了異步編程了。
1.2 .Net中如何使用異步
注意:
其實(shí)當(dāng)我們調(diào)用Begin開頭的方法就是將一個(gè)I/O線程排入到線程池中(調(diào)用Begin開頭的方法就把I/O線程加入到線程池中管理都是.Net機(jī)制幫我們實(shí)現(xiàn)的)。
(因?yàn)橛行┤藭柺裁吹胤接玫搅司€程池了,工作者線程由線程池管理很好看出來,因?yàn)閯?chuàng)建工作者線程直接調(diào)用ThreadPool.QueueUserWorkItem方法來把工作者線程排入到線程池中)。
在.net Framework中的FCL中有許多類型能夠?qū)Ξ惒讲僮魈峁┲С?,其中在FileStream類中就提供了對文件的異步操作的方法。
FileStream類要調(diào)用I/O線程要實(shí)現(xiàn)異步操作,首先要建立一個(gè)FileStream對象。
通過下面的構(gòu)造函數(shù)來初始化FileStream對象實(shí)現(xiàn)異步操作(異步讀取和異步寫入):
public FileStream (string path, FileMode mode, FileAccess access, FileShare share,int bufferSize,bool useAsync)
其中path代表文件的相對路徑或絕對路徑,mode代表如何打開或創(chuàng)建文件,access代表訪問文件的方式,share代表文件如何由進(jìn)程共享,buffersize代表緩沖區(qū)的大小,useAsync代表使用異步I/O還是同步I/O,設(shè)置為true時(shí),說明使用異步I/O.
下面通過代碼來學(xué)習(xí)下異步寫入文件:
using System; using System.IO; using System.Text; using System.Threading; namespace AsyncFile { class Program { static void Main(string[] args) { const int maxsize = 100000; ThreadPool.SetMaxThreads(1000,1000); PrintMessage("Main Thread start"); // 初始化FileStream對象 FileStream filestream = new FileStream("test.txt", FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite, 100, true); //打印文件流打開的方式 Console.WriteLine("filestream is {0} opened Asynchronously", filestream.IsAsync ? "" : "not"); byte[] writebytes =new byte[maxsize]; string writemessage = "An operation Use asynchronous method to write message......................."; writebytes = Encoding.Unicode.GetBytes(writemessage); Console.WriteLine("message size is: {0} byte\n", writebytes.Length); // 調(diào)用異步寫入方法比信息寫入到文件中 filestream.BeginWrite(writebytes, 0, writebytes.Length, new AsyncCallback(EndWriteCallback), filestream); filestream.Flush(); Console.Read(); } // 當(dāng)把數(shù)據(jù)寫入文件完成后調(diào)用此方法來結(jié)束異步寫操作 private static void EndWriteCallback(IAsyncResult asyncResult) { Thread.Sleep(500); PrintMessage("Asynchronous Method start"); FileStream filestream = asyncResult.AsyncState as FileStream; // 結(jié)束異步寫入數(shù)據(jù) filestream.EndWrite(asyncResult); filestream.Close(); } // 打印線程池信息 private static void PrintMessage(String data) { int workthreadnumber; int iothreadnumber; // 獲得線程池中可用的線程,把獲得的可用工作者線程數(shù)量賦給workthreadnumber變量 // 獲得的可用I/O線程數(shù)量給iothreadnumber變量 ThreadPool.GetAvailableThreads(out workthreadnumber, out iothreadnumber); Console.WriteLine("{0}\n CurrentThreadId is {1}\n CurrentThread is background :{2}\n WorkerThreadNumber is:{3}\n IOThreadNumbers is: {4}\n", data, Thread.CurrentThread.ManagedThreadId, Thread.CurrentThread.IsBackground.ToString(), workthreadnumber.ToString(), iothreadnumber.ToString()); } } }
運(yùn)行結(jié)果:
從運(yùn)行結(jié)果可以看出,此時(shí)是調(diào)用線程池中的I/O線程去執(zhí)行回調(diào)函數(shù)的,同時(shí)在工程所的的bin\Debug文件目錄下有生成一個(gè)text.txt文件,打開文件可以知道里面的內(nèi)容正是你寫入的。
下面演示如何從剛才的文件中異步讀取我們寫入的內(nèi)容:
using System; using System.IO; using System.Text; using System.Threading; namespace AsyncFileRead { class Program { const int maxsize = 1024; static byte[] readbytes = new byte[maxsize]; static void Main(string[] args) { ThreadPool.SetMaxThreads(1000, 1000); PrintMessage("Main Thread start"); // 初始化FileStream對象 FileStream filestream = new FileStream("test.txt", FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite, 100, false); // 異步讀取文件內(nèi)容 filestream.BeginRead(readbytes, 0, readbytes.Length, new AsyncCallback(EndReadCallback), filestream); Console.Read(); } private static void EndReadCallback(IAsyncResult asyncResult) { Thread.Sleep(1000); PrintMessage("Asynchronous Method start"); // 把AsyncResult.AsyncState轉(zhuǎn)換為State對象 FileStream readstream = (FileStream)asyncResult.AsyncState; int readlength = readstream.EndRead(asyncResult); if (readlength <=0) { Console.WriteLine("Read error"); return; } string readmessage = Encoding.Unicode.GetString(readbytes, 0, readlength); Console.WriteLine("Read Message is :" + readmessage); readstream.Close(); } // 打印線程池信息 private static void PrintMessage(String data) { int workthreadnumber; int iothreadnumber; // 獲得線程池中可用的線程,把獲得的可用工作者線程數(shù)量賦給workthreadnumber變量 // 獲得的可用I/O線程數(shù)量給iothreadnumber變量 ThreadPool.GetAvailableThreads(out workthreadnumber, out iothreadnumber); Console.WriteLine("{0}\n CurrentThreadId is {1}\n CurrentThread is background :{2}\n WorkerThreadNumber is:{3}\n IOThreadNumbers is: {4}\n", data, Thread.CurrentThread.ManagedThreadId, Thread.CurrentThread.IsBackground.ToString(), workthreadnumber.ToString(), iothreadnumber.ToString()); } } }
運(yùn)行結(jié)果:
這里有個(gè)需要注意的問題:如果大家測試的時(shí)候, 應(yīng)該把開始生成的text.txt文件放到該工程下bin\debug\目錄下, 我剛開始的做的時(shí)候就忘記拷過去的, 讀出來的數(shù)據(jù)長度一直為0(這里我犯的錯(cuò)誤寫下了,希望大家可以注意,也是警惕自己要小心。)
二、I/O線程實(shí)現(xiàn)對請求的異步
我們同樣可以利用I/O線程來模擬對瀏覽器對服務(wù)器請求的異步操作,在.net類庫中的WebRequest類提供了異步請求的支持,
下面就來演示下如何實(shí)現(xiàn)請求異步:
using System; using System.Net; using System.Threading; namespace RequestSample { class Program { static void Main(string[] args) { ThreadPool.SetMaxThreads(1000, 1000); PrintMessage("Main Thread start"); // 發(fā)出一個(gè)異步Web請求 WebRequest webrequest =WebRequest.Create("http://www.cnblogs.com/"); webrequest.BeginGetResponse(ProcessWebResponse, webrequest); Console.Read(); } // 回調(diào)方法 private static void ProcessWebResponse(IAsyncResult result) { Thread.Sleep(500); PrintMessage("Asynchronous Method start"); WebRequest webrequest = (WebRequest)result.AsyncState; using (WebResponse webresponse = webrequest.EndGetResponse(result)) { Console.WriteLine("Content Length is : "+webresponse.ContentLength); } } // 打印線程池信息 private static void PrintMessage(String data) { int workthreadnumber; int iothreadnumber; // 獲得線程池中可用的線程,把獲得的可用工作者線程數(shù)量賦給workthreadnumber變量 // 獲得的可用I/O線程數(shù)量給iothreadnumber變量 ThreadPool.GetAvailableThreads(out workthreadnumber, out iothreadnumber); Console.WriteLine("{0}\n CurrentThreadId is {1}\n CurrentThread is background :{2}\n WorkerThreadNumber is:{3}\n IOThreadNumbers is: {4}\n", data, Thread.CurrentThread.ManagedThreadId, Thread.CurrentThread.IsBackground.ToString(), workthreadnumber.ToString(), iothreadnumber.ToString()); } } }
運(yùn)行結(jié)果為:
寫到這里這篇關(guān)于I/O線程的文章也差不多寫完了, 其實(shí)I/O線程還可以做很多事情,在網(wǎng)絡(luò)(Socket)編程,web開發(fā)中都會用I/O線程,本來想寫個(gè)Demo來展示多線程在實(shí)際的工作中都有那些應(yīng)用的地方的, 但是后面覺得還是等多線程系列都講完后再把知識一起串聯(lián)起來做個(gè)Demo會好點(diǎn),至于后面文章中將介紹下線程同步的問題。
相關(guān)文章
C#難點(diǎn)逐個(gè)擊破(4):main函數(shù)
貌似我是在寫C#的學(xué)習(xí)筆記哦,不過反正可以利用這個(gè)機(jī)會來好好溫習(xí)下基礎(chǔ)知識,這其中很多知識點(diǎn)都屬于平時(shí)視而見的小知識2010-02-02C#中將字符串轉(zhuǎn)換為整型的三種解決方法總結(jié)
本篇文章是對C#中將字符串轉(zhuǎn)換為整型的三種解決方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06C#開發(fā)Winform控件之打開文件對話框OpenFileDialog類
這篇文章介紹了C#開發(fā)Winform控件之打開文件對話框OpenFileDialog類,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-02-02C#中實(shí)現(xiàn)Json序列化與反序列化的幾種方式
C#中實(shí)現(xiàn)Json的序列化與反序列化也算是個(gè)老話題,那么在這篇文章中我們將老話重提,本文中將會學(xué)到如何使用C#,來序列化對象成為Json格式的數(shù)據(jù),以及如何反序列化Json數(shù)據(jù)到對象。有需要的朋友們可以參考借鑒,下面來跟著小編一起學(xué)習(xí)學(xué)習(xí)吧。2016-12-12