服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C# - C#线程处理系列之线程池中的I/O线程

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

2021-11-17 14:27Learning hard C#

这篇文章主要介绍了C#线程处理系列之线程池中的I/O线程,在这篇文章中将介绍如何用线程池中的I/O线程来执行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.

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
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());
    }
  }
}

运行结果:

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

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

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
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());
    }
  }
}

运行结果:

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

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

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

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

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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://www.cnblogs.com/");
      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());
    }
  }
}

运行结果为:

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

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

延伸 · 阅读

精彩推荐
  • C#C#直线的最小二乘法线性回归运算实例

    C#直线的最小二乘法线性回归运算实例

    这篇文章主要介绍了C#直线的最小二乘法线性回归运算方法,实例分析了给定一组点,用最小二乘法进行线性回归运算的实现技巧,具有一定参考借鉴价值,需要...

    北风其凉8912021-10-18
  • C#C# 后台处理图片的几种方法

    C# 后台处理图片的几种方法

    本篇文章主要介绍了C# 后台处理图片的几种方法,非常具有实用价值,需要的朋友可以参考下。...

    IT小伙儿10162021-12-08
  • C#Unity3D UGUI实现缩放循环拖动卡牌展示效果

    Unity3D UGUI实现缩放循环拖动卡牌展示效果

    这篇文章主要为大家详细介绍了Unity3D UGUI实现缩放循环拖动展示卡牌效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参...

    诗远3662022-03-11
  • C#C#实现的文件操作封装类完整实例【删除,移动,复制,重命名】

    C#实现的文件操作封装类完整实例【删除,移动,复制,重命名】

    这篇文章主要介绍了C#实现的文件操作封装类,结合完整实例形式分析了C#封装文件的删除,移动,复制,重命名等操作相关实现技巧,需要的朋友可以参考下...

    Rising_Sun3892021-12-28
  • C#c#学习之30分钟学会XAML

    c#学习之30分钟学会XAML

    一个界面程序的核心,无疑就是界面和后台代码,而xaml就是微软为构建应用程序界面而创建的一种描述性语言,也就是说,这东西是搞界面的...

    C#教程网8812021-12-10
  • C#浅谈C# winForm 窗体闪烁的问题

    浅谈C# winForm 窗体闪烁的问题

    下面小编就为大家带来一篇浅谈C# winForm 窗体闪烁的问题。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧...

    C#教程网7962021-12-21
  • C#聊一聊C#接口问题 新手速来围观

    聊一聊C#接口问题 新手速来围观

    聊一聊C#接口问题,新手速来围观,一个通俗易懂的例子帮助大家更好的理解C#接口问题,感兴趣的小伙伴们可以参考一下...

    zenkey7072021-12-03
  • C#C#基础之泛型

    C#基础之泛型

    泛型是 2.0 版 C# 语言和公共语言运行库 (CLR) 中的一个新功能。接下来通过本文给大家介绍c#基础之泛型,感兴趣的朋友一起学习吧...

    方小白7732021-12-03