zoukankan      html  css  js  c++  java
  • 线程池的使用

    目的:
          能动地有效地处理繁忙的客户段请求。
    做法:
          开启多个线程,使用分流形式处理客户段的请求。
          包括创建自己的线程池和使用系统提供的线程池。

    例子:创建自己的线程池
           
    using System.Threading;

    class MyThreadPool
    {
          ReusableThread[] m_ThreadPool;

          public MyThreadPool(int pSize)
          {
               m_ThreadPool = new ReusableThread[pSize];
               
               for(int i=0; i<pSize; i++)
               {
                    m_ThreadPool[i] = new ReusableThread();
               }
          }

          public ReusableThread GetThread()
          {
               lock(this)
               {
                   ReusableThread vThread = GetAvailableThread();
                   
                   return vThread;
               }
          }

          public ReusableThread GetAvailableThread()
          {
              ReusableThread vReturnThread = null;

              foreach(ReusableThread vThread in m_ThreadPool)
              {
                     if(vThread.IsAvailable)
                     {
                          vThread.IsAvailable = false;
                          vReturnThread = vThread;
                          
                          break;
                     }
              }

              return vReturnThread;
          }

          public void PutThread(ReusableThread pThread)
          {
              lock(this)
              {
                    pThread.IsAvailable = true;
              }
          }
    }
    //--------------------------------ReusableThread-------------------
           public delegate void StartupMethod();
           class ReusableThread
           {
               private Thread m_Thread;
               public bool IsAvailable = true;
               private StartupMethod m_StartupMethod;

               public ReusableThread()
               {
                    m_Thread = new Thread(new ThreadStart(CommonStart));
               }

               public void Join()
               {
                   m_Thread.Join();
               }

               public void Run(StartupMethod pStartupMethod)
               {
                   m_StartupMethod = pStartupMethod;
                   m_Thread.Start();
               }

               public void CommonStart()
               {
                    if(m_StartupMethod != null)
                  {
                        m_StartupMethod();
                        m_StartupMethod 
    = null;
                   }

               }
          }

    //---------------------------Excute code----------------
    public class mainApp
    {
         public void RunWithThread()
         {
              MyThreadPool vPool = new MyThreadPool(20);
              ReusableThread vThead = vPool.GetThread();
              if(vThread == null)
              {
                    return;
              }
              else
              {
                   vThread.Run(new StartupMethod(MyCode));
                   vThread.Join();
                   vPool.PutThread(vThread);
              }
         }
        
          public void MyCode()
          {
               Thread.Sleep(2000);
          }
    }





  • 相关阅读:
    百度文库:网站镜像站点解决方案
    百度文库:WEB网站架构分析HTML静态化
    61条面向对象设计的经验原则
    oracle 定时任务例子【项目例子】
    javascript的事件机制(百度文库)
    【转】Oracle回收站(recyclebin)
    java十大低级错误和常见注意点
    JAVA UUID 生成
    Oracle in和exists效率问题分析
    http的长连接和短连接(数据库也一样)
  • 原文地址:https://www.cnblogs.com/si812cn/p/1223011.html
Copyright © 2011-2022 走看看