INDY9線程池機理

針對INDY9版本

// 使用完的線程歸還線程池

procedure TIdThreadMgrPool.ReleaseThread(AThread: TIdThread);

var

  LThreadPool: TList;

begin

  ActiveThreads.Remove(AThread);

  LThreadPool := FThreadPool.LockList;

  try

  // PoolSize = 0 means that we will keep all active threads in the thread pool

    if ((PoolSize > 0) and (LThreadPool.Count >= PoolSize)) or AThread.Terminated then begin

      if IsCurrentThread(AThread) then begin

        AThread.FreeOnTerminate := True;

        AThread.Terminate;

      end else begin

        if not AThread.Stopped then

        begin

          AThread.TerminateAndWaitFor;

        end;

        AThread.Free;

      end;

    end else begin

      if not AThread.Suspended then begin

        AThread.OnStopped := ThreadStopped;

        AThread.Stop;

      end

      else begin

        AThread.Free;

      end;

    end;

  finally FThreadPool.UnlockList; end;

end;

 

創建線程池的時候不會一運行就創建N個線程

 

當一個客戶第一次建立連接,才會在池內創建一個線程,用完後掛起,直至線程數量 > poolsize,所以poolsize不要設的過大。

poolsize=0 線程池完全不起作用

 

 

posted @ 2009-06-30 16:22  delphi中间件  阅读(288)  评论(0编辑  收藏  举报