好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

C#线程处理系列之线程池中的I/O线程

一、i/o线程实现对文件的异步

 1.1  i/o线程介绍:

对于线程所执行的任务来说,可以把线程分为两种类型:工作者线程和i/o线程。

工作者线程用来完成一些计算的任务,在任务执行的过程中,需要cpu不间断地处理,所以,在工作者线程的执行过程中,cpu和线程的资源是充分利用的。

i/o线程主要用来完成输入和输出的工作的,在这种情况下, 计算机需要i/o设备完成输入和输出的任务,在处理过程中,cpu是不需要参与处理过程的,此时正在运行的线程将处于等待状态,只有等任务完成后才会有事可做, 这样就造成线程资源浪费的问题。为了解决这样的问题,可以通过线程池来解决这样的问题,让线程池来管理线程,前面已经介绍过线程池了, 在这里就不讲了。

对于i/o线程,我们可以将输入输出操作分成三个步骤:启动、实际输入输出、处理结果。用于实际输入输出可由硬件完成,并不需要cpu的参与,而启动和处理结果也可以不在同一个线程上,这样就可以充分利用线程资源。在.net中通过以begin开头的方法来完成启动,以end开头的方法来处理结果,这两个方法可以运行在不同的线程,这样我们就实现了异步编程了。

1.2 .net中如何使用异步

注意:

 其实当我们调用begin开头的方法就是将一个i/o线程排入到线程池中(调用begin开头的方法就把i/o线程加入到线程池中管理都是.net机制帮我们实现的)。

(因为有些人会问什么地方用到了线程池了,工作者线程由线程池管理很好看出来,因为创建工作者线程直接调用threadpool.queueuserworkitem方法来把工作者线程排入到线程池中)。

在.net framework中的fcl中有许多类型能够对异步操作提供支持,其中在filestream类中就提供了对文件的异步操作的方法。

filestream类要调用i/o线程要实现异步操作,首先要建立一个filestream对象。

通过下面的构造函数来初始化filestream对象实现异步操作(异步读取和异步写入):

public filestream (string path, filemode mode, fileaccess access, fileshare share,int buffersize,bool useasync)

其中path代表文件的相对路径或绝对路径,mode代表如何打开或创建文件,access代表访问文件的方式,share代表文件如何由进程共享,buffersize代表缓冲区的大小,useasync代表使用异步i/o还是同步i/o,设置为true时,说明使用异步i/o.

下面通过代码来学习下异步写入文件:

?

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);

       // 调用异步写入方法比信息写入到文件中

       filestream.beginwrite(writebytes, 0, writebytes.length, new asynccallback(endwritecallback), filestream);

       filestream.flush();

       console.read();

 

     }

 

     // 当把数据写入文件完成后调用此方法来结束异步写操作

     private static void endwritecallback(iasyncresult asyncresult)

     {

       thread.sleep(500);

       printmessage( "asynchronous method start" );

 

       filestream filestream = asyncresult.asyncstate as filestream;

 

       // 结束异步写入数据

       filestream.endwrite(asyncresult);

       filestream.close();

     }

 

     // 打印线程池信息

     private static void printmessage( string data)

     {

       int workthreadnumber;

       int iothreadnumber;

 

       // 获得线程池中可用的线程,把获得的可用工作者线程数量赋给workthreadnumber变量

       // 获得的可用i/o线程数量给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());

     }

   }

}

运行结果:

从运行结果可以看出,此时是调用线程池中的i/o线程去执行回调函数的,同时在工程所的的bin\debug文件目录下有生成一个text.txt文件,打开文件可以知道里面的内容正是你写入的。

下面演示如何从刚才的文件中异步读取我们写入的内容:

?

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 );

 

       // 异步读取文件内容

       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转换为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;

 

       // 获得线程池中可用的线程,把获得的可用工作者线程数量赋给workthreadnumber变量

       // 获得的可用i/o线程数量给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());

     }

   }

}

运行结果:

这里有个需要注意的问题:如果大家测试的时候, 应该把开始生成的text.txt文件放到该工程下bin\debug\目录下, 我刚开始的做的时候就忘记拷过去的, 读出来的数据长度一直为0(这里我犯的错误写下了,希望大家可以注意,也是警惕自己要小心。)

二、i/o线程实现对请求的异步

我们同样可以利用i/o线程来模拟对浏览器对服务器请求的异步操作,在.net类库中的webrequest类提供了异步请求的支持,

下面就来演示下如何实现请求异步:

?

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" );

 

       // 发出一个异步web请求

       webrequest webrequest =webrequest.create( "http://HdhCmsTestcnblogs测试数据/" );

       webrequest.begingetresponse(processwebresponse, webrequest);

 

       console.read();

     }

 

     // 回调方法

     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;

 

       // 获得线程池中可用的线程,把获得的可用工作者线程数量赋给workthreadnumber变量

       // 获得的可用i/o线程数量给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());

     }

   }

}

运行结果为:

 

写到这里这篇关于i/o线程的文章也差不多写完了, 其实i/o线程还可以做很多事情,在网络(socket)编程,web开发中都会用i/o线程,本来想写个demo来展示多线程在实际的工作中都有那些应用的地方的, 但是后面觉得还是等多线程系列都讲完后再把知识一起串联起来做个demo会好点,至于后面文章中将介绍下线程同步的问题。

dy("nrwz");

查看更多关于C#线程处理系列之线程池中的I/O线程的详细内容...

  阅读:48次