Thread Pool Example in delphi

2

How do I make a pool of Thread , I need to execute a process that contains several records, but I need to send on demand, send 10 and as I release it, send more ... how can I do it? / p>

I've set an example ...

unit Unit1;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;

type
   MinhaThread = class(TThread)
   procedure Execute; override;
   procedure Verifica;
   procedure Fechar;
   Private
   constructor Create();
end;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Memo1: TMemo;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
    thread: MinhaThread;
  public
    { Public declarations }
    procedure consultaProcesso(Sender: TObject);
    procedure postJSON(JSON:String);
  end;

var
  Form1: TForm1;
  I : Integer;
  JSON:String;

implementation

{$R *.dfm}

{ MinhaThread }

constructor MinhaThread.Create;
begin
inherited
  Create(True);
  FreeOnTerminate := True;
  Priority := tpLower;
  Resume;
end;

procedure MinhaThread.Execute;
Var Sender : TObject;
begin
   Synchronize(Verifica);
   Form1.consultaProcesso(Sender); // Executar Rotina ( Procedures )

   while not Terminated do
    begin
        Sleep (10);
        Terminate; // Finaliza a Thread
        Synchronize(Fechar);
    end;

end;

procedure MinhaThread.Fechar;
begin
  //application.terminate;
end;

procedure MinhaThread.Verifica;
begin
  Form1.Caption := 'EXECUTANDO...'+IntToStr(I);
end;

{ TForm1 }

procedure TForm1.Button1Click(Sender: TObject);
begin
  thread := MinhaThread.Create();
end;

procedure TForm1.consultaProcesso(Sender: TObject);
begin
  //exemplo com o for
  // porem aqui eu percorro a query, passo 
  // para a variavel JSON o json que está na query
  // e chamo o metodo post
  for I := 0 to 100 do
    begin
      postJSON(JSON);
    end;
end;

procedure TForm1.postJSON(JSON: String);
begin
  //faz um post pelo idHTTP;
  Memo1.Lines.Add(DateTimeToStr(now)+ ' - Executando JSON '+IntToStr(I));
  //para simular um tempo de espera do retorno
  Sleep(1000);
  //retorno...
  Memo1.Lines.Add(DateTimeToStr(now)+ ' - Retorno JSON '+IntToStr(I));
end;

end.
In the consultaProcesso method, I put a for , but it would be the same as the while I do in query by taking JSON and passing to method postJSON , in this method I put sleep to simulate a return time that I have from post done by idHTTP .

    
asked by anonymous 13.06.2017 / 14:16

1 answer

2

See if this process helps resolve your issue:

var
  MinhasThreads: Array of MinhaThread;
  i: Integer;
begin

  SetLength(MinhasThreads, 0);

  // Criando as threads dinâmicamente
  for i := 0 to 9 do
    begin
      SetLength(MinhasThreads, Length(MinhasThreads) + 1);
      MinhasThreads[High(MinhasThreads)] := MinhaThread.Create(True);
      MinhasThreads[High(MinhasThreads)].FreeOnTerminate := True;
      MinhasThreads[High(MinhasThreads)].Start;
    end;

  // Verificar se existe alguma Thread em execução
  //O sistema irá ficar rodando esse laço de repetição até que todas as threads sejam finalizadas.
  i := 0;
  while (i <= High(MinhasThreads)) do
    begin
      if (MinhasThreads[i] <> nil) then
        i := 0
      else
        Inc(i);
    end;
end;

The idea is to create all threads within an array, these threads are configured to self-destruct as soon as the execute procedure is finalized.

    
13.06.2017 / 20:25