电信主站 网通分站
购买流程 付款方式 常见问题 在线提问 续租服务 购物车
用户名: 密 码: 忘记密码?
首 页
域名注册
虚拟主机
双线主机
服务器租用
VPS主机
企业邮局
代理专区
客服中心
虚拟主机行业资讯 虚拟主机评测对比 互联网最新动态 技术学院 站长资讯 在线教程 网站运营
搜索优化 服务器 网络编程 图形图象 站长之家 网页制作 操作系统
冲浪宝典 软件教学 视频通信 办公软件 邮件系统 网络安全 认证考试
您当前位置:西部数码->资讯中心-> 在线教程-> .NET
custom thread pooling template-.net教程,算法/线
作者:网友供稿 点击:17
  西部数码-全国虚拟主机10强!20余项虚拟主机管理功能,全国领先!第6代双线路虚拟主机,南北访问畅通无阻!虚拟主机可在线rar解压,自动数据恢复设置虚拟目录等.虚拟主机免费赠送访问统计,企业邮局.Cn域名注册10元/年,自助建站480元起,免费试用7天,满意再付款!P4主机租用799元/月.月付免压金!
文章页数:[1] 
custom thread pooling template
now that we have covered the basics of visual studio .net project types and templates, let us look at how we can write our own custom project template that will generate a thread pool that can be used by the developer without any modifications at all. before we start discussing the custom template, let us focus on writing a thread pool class that will be a part of this template.
a thread pool usually consists of two classes: one class that represents a thread, and another class that represents the object that manages a pool of these thread objects. the thread class is fairly simple: it is simply an encapsulation of the built in system.threading.thread class, but the thread pool class is slightly more involved. this thread pool class has to create all the threads in the pool, assign them work, and keep track of idle threads so that further work can be assigned to them. further, the threads in a thread pool are created once, and if a thread is idle, the thread goes into an efficient wait mode (like a sleep call). when there is work to be processed, the thread pool is responsible for bringing an idle thread back into active mode and assigning it work.
let us start by discussing the thread class first.
public class threadpoolthread
{
   public threadpoolthread
    (threadpool ithreadpool, threadpool.dowork iworkermethod)
   {
      mworkermethod += iworkermethod;

      mthreadpool = ithreadpool;
      mthread = new thread (new threadstart (threadfunc));
      mevent = new autoresetevent (false);
      mthread.start ();
   }

   public void processrequest ()
   {
      mevent.set ();
   }

   public void stop ()
   {
      if (mthread != null)
         mthread.abort ();
   }

   private void threadfunc ()
   {
      waithandle[] lwaithandle = new waithandle[1];
      lwaithandle[0] = mevent;

      while (true){
         autoresetevent.waitall (lwaithandle);
         mworkermethod ();
         mthreadpool.returntopool (this);
      }
   }

   private threadpool.dowork mworkermethod;
   private thread mthread;
   private autoresetevent mevent;
   private threadpool mthreadpool;
}
this class has four methods, a constructor that sets up the objects required by the class, a method to wake up the thread, a method to kill the thread, and the worker method where the thread will spend its entire life. the threadpoolthread object consists of one thread object and one autoresetevent object, which is used to signal an idle thread. the constructor takes in a reference to the thread pool that this thread belongs to, and it also takes in a delegate which is the user defined method that this thread will invoke as part of its processing. the user of this class will pass this delegate, and it typically will be a method that will accept a request (from a socket or from a web service, etc) and process it. once this delegate method returns, the thread will go into an efficient wait, blocking the event object. this is an efficient wait state that consumes minimal resources. the thread can be brought out of this wait state by the thread pool, by calling its processrequest method. this method simply calls the set method of the event object, which causes the event object to go into a signaled state. this causes the autoresetevent.waitall method to come out of a blocking state, and invoke the delegate method. this continues in an infinite loop, until the thread is terminated by the thread pool calling the stop method.
let us now look at the thread pool class. note that this is a custom implementation of the thread pool, which is different from the system.threading.threadpool class. we will start off by discussing the start and the stop methods.
public delegate void dowork ();

public threadpool (int inumthreads)
{
   mnumthreads = inumthreads;
}

public void start (dowork iworkermethod)
{
   mfreelist = new threadpoolthread [mnumthreads];
   mthreads = new threadpoolthread [mnumthreads];
   mfreethreads = mnumthreads;

   threadpoolthread lthread;

   for (int i = 0; i < mnumthreads; i++){
      lthread = new threadpoolthread (this, iworkermethod);
      mfreelist[i] = mthreads[i] = lthread;
   }
}

public void stop ()
{
   for (int i = 0; i < mnumthreads; i++)
      mthreads[i].stop ();
}

private int mnumthreads;
private threadpoolthread[] mfreelist;
private threadpoolthread[] mthreads;
private int mfreethreads;
the first thing that the threadpool class defines is a delegate that is used by the thread class to call the method that will perform the actual processing, as explained above. the constructor of the thread pool takes in a single parameter which specifies the number of threads that will be created in this pool. the thread pool is started by calling the start method, which creates a pool of threadpoolthread objects, and stores a pointer reference to these objects in the mthreads and mfreelist arrays (we use arrays for simplicity in this sample: a more efficient alternative would be the use of lists). the mthreads array represents all the threads in this pool, and the mfreelist array represents all the threads that are currently free to service requests. all threads start off belonging to the free list. the stop method simply calls the stop method on the threadpoolthread class that aborts the thread.
let us now look at the processrequest method that is called by the client when it needs a thread to process a request. calling this method causes the delegate that was passed to the start method to be called.
public bool processrequest ()
{
   threadpoolthread lthread = null;
   if (mfreethreads == 0)
      return false;

   lock(this)
   {
      mfreethreads--;
      for (int i = 0; i < mnumthreads; i++)
         if (mfreelist[i] != null){
            lthread = mfreelist[i];
            mfreelist[i] = null;
            break;
         }
   }

   lthread.processrequest ();
   return true;
}
the processrequest method needs to find a free thread to process the request, and it starts off by checking the value of the mfreethreads counter, which indicates the number of available free threads. if no threads are available, the methods returns false. if free threads are available, the thread pool iterates through the mfreelist array looking for the first free thread. once a thread is found, it marks the thread as busy by removing it from the free list. this method then calls the processrequest method on the free thread, that in turns invokes the user delegate. since this method can be called from multiple threads, we use the c# lock keyword to protect the mfreelist and mfreethreads objects for concurrent access.
the returntopool method that is used to return a busy thread to the pool of free threads is shown below. this method is similar to the processrequest method, wherein the method attempts to find a free position in the mfreelist array to add the thread to. again, we use the lock keyword to guard against concurrent access to the mfreelist and mfreethreads objects.
public void returntopool (threadpoolthread ithread)
{
   lock (this)
   {
      mfreethreads++;
      for (int i = 0; i < mnumthreads; i++)
         if (mfreelist[i] == null){
            mfreelist[i] = ithread;
            break;
         }
   }

   return;
}
   
文章整理:西部数码--专业提供域名注册虚拟主机服务
http://www.west263.com
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!
相关主题
文章页数:[1] 
Google
热门文章
·经典收藏之 - C++内存管理详解-.NET教程,C#语言
·Master Page 初探-.NET教程,评论及其它
·GDI+编程10个基本技巧-.NET教程,评论及其它
·VB.NET中让Textbox只能输入数字(二)-.NET教程,VB.Net语言
·stl应用小问题-.NET教程,评论及其它
·WIN32中颜色值(COLORREF)与.NET中颜色值(Color)的转换-ASP教程,系统相关
·打造自己的专业图像工具-Visual C++ 2005图像编程系列【三】-.NET教程,C#语言
·.Net中常见问题及解决方法归类-.NET教程,.NET Framework
·Lex和Yacc从入门到精通(3)--一个极其简单的lex和yacc程序-.NET教程,评论及其它
·VB下几个非常有用的函数-.NET教程,VB.Net语言

最新文章
·VC#初学入门:第一个Windows程序
·ASP.NET 2.0-选用DataSet或DataReader
·用.net 处理xmlHttp发送异步请求
·asp.net创建文件夹的IO类的问题
·asp.net 2.0 中加密web.config 文件中的配置节
·关于ASP.NET调用JavaScript的实现
·如何实现ASP.NET网站个性化
·Acegi安全系统的配置-.NET教程,评论及其它
·Spring安全系统:Acegi Security Acegi简介-.NET教程,评论及其它
·Biztalk 开发之 架构和实例的验证-.NET教程,评论及其它




版权申明:本站文章均来自网络,如有侵权,请联系我们,我们收到后立即删除,谢谢!

特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有。
  打印  刷新  关闭
返回首页 |关于我们 | 联系我们 | 付款方式 | 创业联盟 | 虚拟主机 | 资讯中心 | 友情链接 | 网站地图

版权所有 西部数码(www.west263.com)
CopyRight (c) 2002~2006 west263.com all right reserved.
公司地址:四川成都市万和路90号天象大厦4楼 邮编:610031
电话总机:028-86262244 86263048 86263408 86263960 86264018 86267838
售前咨询:总机转201 202 203 204 206 208
售后服务:总机转211 212 213 214
财务咨询:总机转224 223 传真:028-86264041 财务QQ:点击发送消息给对方635483282
售前咨询QQ:点击发送消息给对方2182518 点击发送消息给对方241975952 点击发送消息给对方275026793 点击发送消息给对方408235859
售后服务QQ:点击发送消息给对方17708515 点击发送消息给对方307742704 点击发送消息给对方287976517 点击发送消息给对方363783715
《中华人民共和国增值电信业务经营许可证》编号:川B2-20030065号