欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

解析C#多线程编程中异步多线程的实现及线程池的使用

程序员文章站 2022-05-14 17:43:50
0、线程的本质 线程不是一个计算机硬件的功能,而是操作系统提供的一种逻辑功能,线程本质上是进程中一段并发运行的代码,所以线程需要操作系统投入cpu资源来运行和调度。 1...

0、线程的本质
线程不是一个计算机硬件的功能,而是操作系统提供的一种逻辑功能,线程本质上是进程中一段并发运行的代码,所以线程需要操作系统投入cpu资源来运行和调度。

1、多线程:

使用多个处理句柄同时对多个任务进行控制处理的一种技术。据博主的理解,多线程就是该应用的主线程任命其他多个线程去协助它完成需要的功能,并且主线程和协助线程是完全独立进行的。不知道这样说好不好理解,后面慢慢在使用中会有更加详细的讲解。

2、多线程的使用:

(1)最简单、最原始的使用方法:thread ogetargthread = new thread(new threadstart(() =>{});这种用法应该大多数人都使用过,参数为一个threadstart类型的委托。将threadstart转到定义可知:

public delegate void threadstart();

它是一个没有参数,没有返回值的委托。所以他的使用如下:

static void main(string[] args)
{
   thread ogetargthread = new thread(new threadstart(test));
  ogetargthread.isbackground = true;
  ogetargthread.start();  
    for (var i = 0; i < 1000000; i++)
    {
      console.writeline("主线程计数" + i);
      //thread.sleep(100);
    }

}

private static void test()
 {
    for (var i = 0; i < 1000000; i++)
    {
      console.writeline("后台线程计数" + i);
      //thread.sleep(100);
    }
 }

定义一个没有参数没有返回值的方法传入该委托。当然也可以不定义方法写成匿名方法:

    static void main(string[] args)
    {
      thread ogetargthread = new thread(new system.threading.threadstart(() =>
      {
        
        for (var i = 0; i < 1000000; i++)
        {
          console.writeline("后台线程计数" + i);
          //thread.sleep(100);
        }
      }));
      ogetargthread.isbackground = true;
      ogetargthread.start();

这个和上面的意义相同。得到的结果如下:

解析C#多线程编程中异步多线程的实现及线程池的使用

说明主线程和后台线程是互相独立的。由系统调度资源去执行。

如果这样那有人就要问了,如果我需要多线程执行的方法有参数或者有返回值或者既有参数又有返回值呢。。。别着急我们来看看new thread()的几个构造函数:

public thread(parameterizedthreadstart start);
    public thread(threadstart start);
    public thread(parameterizedthreadstart start, int maxstacksize);
    public thread(threadstart start, int maxstacksize);

转到定义可知参数有两类,一类是无参无返回值的委托,另一类是有参无返回值的委托。对于有参数的委托使用方法:

    static void main(string[] args)
    {
      thread othread = new thread(new parameterizedthreadstart(test2));   
      othread.isbackground = true;
      othread.start(1000);
     }

     private static void test2(object count)
    {
      for (var i = 0; i < (int)count; i++)
      {
        console.writeline("后台线程计数" + i);
        //thread.sleep(100);
      }
    }  

 

对于有参又有返回值的委托,很显然使用new thread()这种方式是没有解决方案的。其实对于有参又有返回值的委托可以使用异步来实现:

public delegate string methodcaller(string name);//定义个代理 
methodcaller mc = new methodcaller(getname); 
string name = "my name";//输入参数 
iasyncresult result = mc.begininvoke(name,null, null); 
string myname = mc.endinvoke(result);//用于接收返回值 
 
public string getname(string name)  // 函数
{
  return name;
}  

关于这种方式还有几点值得一说的是:

thread ogetargthread = new thread(new threadstart(test));

ogetargthread.join();//主线程阻塞,等待分支线程运行结束,这一步看功能需求进行选择,主要为了多个进程达到同步的效果

②线程的优先级可以通过thread对象的priority属性来设置,priority属性对应一个枚举:

public enum threadpriority
  {
    // 摘要: 
    //   可以将 system.threading.thread 安排在具有任何其他优先级的线程之后。
    lowest = 0,
    //
    // 摘要: 
    //   可以将 system.threading.thread 安排在具有 normal 优先级的线程之后,在具有 lowest 优先级的线程之前。
    belownormal = 1,
    //
    // 摘要: 
    //   可以将 system.threading.thread 安排在具有 abovenormal 优先级的线程之后,在具有 belownormal 优先级的线程之前。
    //   默认情况下,线程具有 normal 优先级。
    normal = 2,
    //
    // 摘要: 
    //   可以将 system.threading.thread 安排在具有 highest 优先级的线程之后,在具有 normal 优先级的线程之前。
    abovenormal = 3,
    //
    // 摘要: 
    //   可以将 system.threading.thread 安排在具有任何其他优先级的线程之前。
    highest = 4,
  }

从0到4,优先级由低到高。

③关于多个线程同时使用一个对象或资源的情况,也就是线程的资源共享,为了避免数据紊乱,一般采用.net悲观锁lock的方式处理。

     private static object olock = new object();
    private static void test2(object count)
    {
      lock (olock)
      {
        for (var i = 0; i < (int)count; i++)
        {
          console.writeline("后台线程计数" + i);
          //thread.sleep(100);
        }
      }
    }

 

(2)task方式使用多线程:

这种方式一般用在需要循环处理某项业务并且需要得到处理后的结果。使用代码如下:

list<task> lsttaskbd = new list<task>();
foreach (var bd in lstboards)
  {
     var bdtmp = bd;//这里必须要用一个临时变量
     var otask = task.factory.startnew(() =>
     {
       var strcpbdcmd = "rm -rf " + bdtmp.path + "/*;cp -r " + combineftppaths(ftp_emulation_bd_root,

"bd_correct") + "/* " + bdtmp.path + "/";
       oplink.run(bdtmp.emulationserver.bigip, bdtmp.emulationserver.username, bdtmp.emulationserver.password,

strcpbdcmd);
       thread.sleep(500);
      });
      lsttaskbd.add(otask);
  }
task.waitall(lsttaskbd.toarray());//等待所有线程只都行完毕

使用这种方式的时候需要注意这一句 var bdtmp = bd;这里必须要用一个临时变量,要不然多个bd对象容易串数据。如果有兴趣可以调试看看。这种方法比较简单,就不多说了。当然task对象的用法肯定远不止如此,还涉及到任务的调度等复杂的逻辑。博主对这些东西理解有限,就不讲解了。

 (3)异步操作的本质
  所有的程序最终都会由计算机硬件来执行,所以为了更好的理解异步操作的本质,我们有必要了解一下它的硬件基础。 熟悉电脑硬件的朋友肯定对dma这个词不陌生,硬盘、光驱的技术规格中都有明确dma的模式指标,其实网卡、声卡、显卡也是有dma功能的。dma就是直 接内存访问的意思,也就是说,拥有dma功能的硬件在和内存进行数据交换的时候可以不消耗cpu资源。只要cpu在发起数据传输时发送一个指令,硬件就开 始自己和内存交换数据,在传输完成之后硬件会触发一个中断来通知操作完成。这些无须消耗cpu时间的i/o操作正是异步操作的硬件基础。所以即使在dos 这样的单进程(而且无线程概念)系统中也同样可以发起异步的dma操作。

(4)异步操作的优缺点
  因为异步操作无须额外的线程负担,并且使用回调的方式进行处理,在设计良好的情况下,处理函数可以不必使用共享变量(即使无法完全不用,最起码可以减少 共享变量的数量),减少了死锁的可能。当然异步操作也并非完美无暇。编写异步操作的复杂程度较高,程序主要使用回调方式进行处理,与普通人的思维方式有些出入,而且难以调试。

3、线程池的用法:

一般由于考虑到服务器的性能等问题,保证一个时间段内系统线程数量在一定的范围,需要使用线程池的概念。大概用法如下:

  public class cspiderctrl
  {

     //将线程池对象作为一个全局变量
    static semaphore semaphore;

    public static void run()
    {
      //1. 创建 superlcbb客户端对象
      var oclient = new servicereference_superlcbb.soaserviceclient();

       //2.初始化的时候new最大的线程池个数255(这个数值根据实际情况来判断,如果服务器上面的东西很少,则可以设置大点)
      semaphore = new semaphore(250, 255);

      clogservice.instance.debug("又一轮定时采集...");

      _testbedgo(oclient);

    }

 

   //执行多线程的方法

   private static void _testbedgo(servicereference_superlcbb.soaserviceclient oclient)
    {
      list<string> lstexceptpdus = new list<string>(){
        "superlabexp"
      };
      var otestbedres = oclient.gettestbedexceptsomepdu(lstexceptpdus.toarray(), true);
      if (ckvres.errcode_success != otestbedres.errcode)
      {
        clogservice.instance.error("xxx");
        return;
      }

      var lsttestbed = otestbedres.todocumentsex();

      system.threading.tasks.parallel.foreach(lsttestbed, (otestbed) =>
      {

         //一次最多255个线程,超过255的必须等待线程池释放一个线程出来才行
        semaphore.waitone();

        //clogservice.instance.info("开始采集测试床:" + otestbed[tbltestbed.prop_name]);
        //thread.sleep(2000);

        var strtestbedname = otestbed[tbltestbed.prop_name] as string;
        var strsuperdevip = otestbed[tbltestbed.prop_superdevip] as string;
        var strtestbedgid = otestbed[tbltestbed.prop_gid] as string;
        var strpdu = otestbed[tbltestbed.prop_pdugid] as string;
        thread.sleep(new random().next(1000, 5000));
        var ogetrootdevicesbytestbedgidres = oclient.getrootdevicesbytestbedgid(strtestbedgid);
        clogservice.instance.debug(strpdu + "——测试床name:" + strtestbedname + "开始");
        stopwatch sp = new stopwatch();
        sp.start();
        if (ogetrootdevicesbytestbedgidres.errcode != ckvres.errcode_success || ogetrootdevicesbytestbedgidres.documents.count < 2)
        {
          clogservice.instance.debug("shit -- 3实验室中测试床name:" + strtestbedname + "2完成异常0");

       //这里很重要的一点,每一次return 前一定要记得释放线程,否则这个一直会占用资源
          semaphore.release();
          return;
        }


        var strxml = ogetrootdevicesbytestbedgidres.documents[0];
        var strexename = ogetrootdevicesbytestbedgidres.documents[1];
        //var strexename = "ratespider";


        var osuperdevclient = new superdevclient(csuperdev.endpoint, string.format(csuperdev.superdevurl, strsuperdevip));
        try
        {
          osuperdevclient.isok();
        }
        catch (exception)
        {
          clogservice.instance.error("测试床name:" + strtestbedname + "异常,插件没起");
          semaphore.release();
          return;
        }


        //2.3.1.请求superdev.server(superdevip),发送run(xml和exename)
        var orunexeres = new ckvres();
        try
        {
          orunexeres = osuperdevclient.runexeex(strexename, false, new string[] { strxml });
        }
        catch
        {
          //clogservice.instance.debug("测试床name:" + strtestbedname + "异常:" + ex.message);
        }
        sp.stop();
        clogservice.instance.debug(strpdu + "——测试床name:" + strtestbedname + "完成时间" + sp.elapsed);

          //每一个线程完毕后记得释放资源
        semaphore.release();
      });
    }

  }

需要注意:semaphore对象的数量需要根据服务器的性能来设定;system.threading.tasks.parallel.foreach这种方式表示同时启动lsttestbed.length个线程去做一件事情,可以理解为

foreach(var otestbed in lsttestbed)
{
    thread othread=new thread(new threadstart({  ...}));     
}

 

(4) 多线程里面还有一个值得一说的spinwait类,用于提供对基于自旋的等待的支持。也就是说支持重复执行一个委托,知道满足条件就返回,我们来看它的用法:

    public static void spinuntil(func<bool> condition);
   
    public static bool spinuntil(func<bool> condition, int millisecondstimeout);
   
    public static bool spinuntil(func<bool> condition, timespan timeout);

这个方法有三个构造函数,后两个需要传入一个时间,表示如果再规定的时间内还没有返回则自动跳出,防止死循环。

            spinwait.spinuntil(() =>
          {
            bisworking = m_oclient.isworking(new isworking()).result;
            return bisworking == false;
          }, 600000);
          //如果等了10分钟还在跳纤则跳出
          if (bisworking)
          {
            ores.errcode = "false交换机跳纤时间超过10分钟,请检查异常再操作";
            return ores;
          }

4、多线程的优缺点
多线程的优点很明显,线程中的处理程序依然是顺序执行,符合普通人的思维习惯,所以编程简单。但是多线程的缺点也同样明显,线程的使用(滥用)会给系统带来上下文切换的额外负担。并且线程间的共享变量可能造成死锁的出现。

5、适用范围
在了解了线程与异步操作各自的优缺点之后,我们可以来探讨一下线程和异步的合理用途。我认为:当需要执行i/o操作时,使用异步操作比使用线程+同步 i/o操作更合适。i/o操作不仅包括了直接的文件、网络的读写,还包括数据库操作、web service、httprequest以及.net remoting等跨进程的调用。

而线程的适用范围则是那种需要长时间cpu运算的场合,例如耗时较长的图形处理和算法执行。但是往往由于使用线程编程的简单和符合习惯,所以很多朋友往往会使用线程来执行耗时较长的i/o操作。这样在只有少数几个并发操作的时候还无伤大雅,如果需要处理大量的并发操作时就不合适了。