Hi guys,
I have 8 threads that assign to 5 jobs, job 1 & job 2 each one, assign 1 thread, while job 4-5, each one assign 2 threads,see picture 1 (Th1.png).
Job 1 is the lightest, so its thread finished first. Job 5 is the heaviest one.
I want to finished the jobs faster, so I divided job 5 into 3 sections and plan to assign 3 threads to this job, but not assign all three threads immediately, first assign 2 threads to process section 1 & 2, and then, when thread from job 1 finished, assign another thread to process section 3 in job 5, see picture 2 (Th2.png).
I thought by adding more threads to job5, all jobs will finished faster, but it was not, in fact, it was slower by a couple of seconds.
Here are the thread declaration
TGetDtThr = class(TThread)
private
public
IdLoc: string;
Range1,Range2:integer;
Name:string;
constructor Create(CreateSuspended: boolean; AThreadDone: TNotifyEvent); overload;
protected
procedure Execute; override;
end;
Threads codes
constructor TGetDtThr.Create(CreateSuspended: boolean; AThreadDone: TNotifyEvent);
begin
inherited Create(true);
FreeOnTerminate := true;
OnTerminate := AThreadDone;
ThreadsRunning := ThreadsRunning + 1;
MainFrm.LblThr.Caption := IntToStr(ThreadsRunning);
end;
procedure TGetDtThr.Execute;
begin
mainfrm.StartThread(IdLoc,Range1,Range2);
end;
Codes to execute picture 1 in MainFrm.pas
procedure TMainFrm.Go();
var
Dt1,Dt2,Dt3,Dt4,Dt5,Dt6,Dt7,Dt8 : TGetDtThr;
begin
Dt1 := TGetDtThr.Create( True,@ThreadDone);
Dt1.IdLoc := 'Job1';
Dt1.Range1 := 1;
Dt1.Range2 := 30;
Dt1.Name := 'T1';
Dt1.Start;
Dt2 := TGetDtThr.Create( True,@ThreadDone);
Dt2.IdLoc := 'Job2';
Dt2.Range1 := 1;
Dt2.Range2 := 30;
Dt2.Name := 'T2';
Dt2.Start;
Dt3 := TGetDtThr.Create( True,@ThreadDone);
Dt3.IdLoc := 'Job3';
Dt3.Range1 := 1;
Dt3.Range2 := 15;
Dt3.Name := 'T3';
Dt3.Start;
Dt4 := TGetDtThr.Create( True,@ThreadDone);
Dt4.IdLoc := 'Job3';
Dt4.Range1 := 16;
Dt4.Range2 := 30;
Dt4.Name := 'T4';
Dt4.Start;
Dt5 := TGetDtThr.Create( True,@ThreadDone);
Dt5.IdLoc := 'Job4';
Dt5.Range1 := 1;
Dt5.Range2 := 15;
Dt5.Name := 'T5';
Dt5.Start;
Dt6 := TGetDtThr.Create( True,@ThreadDone);
Dt6.IdLoc := 'Job4';
Dt6.Range1 := 16;
Dt6.Range2 := 30;
Dt6.Name := 'T6';
Dt6.Start;
Dt7 := TGetDtThr.Create( True,@ThreadDone);
Dt7.IdLoc := 'Job5';
Dt7.Range1 := 1;
Dt7.Range2 := 15;
Dt7.Name := 'T7';
Dt7.Start;
Dt8 := TGetDtThr.Create( True,@ThreadDone);
Dt8.IdLoc := 'Job5';
Dt8.Range1 := 16;
Dt8.Range2 := 30;
Dt8.Name := 'T8';
Dt8.Start;
end;
procedure TMainFrm.ThreadDone(Sender: TObject);
begin
ThreadsRunning := ThreadsRunning-1;
lblThr.Caption:= IntToStr(ThreadsRunning);
if (ThreadsRunning <= 0) then
begin
ShowMessage('Finished');
end;
end;
Codes to execute picture 2.
The different is on procedure Go in Mainfrm, it look like these :
Dt7 := TGetDtThr.Create( True,@ThreadDone);
Dt7.IdLoc := 'Job5';
Dt7.Range1 := 1;
Dt7.Range2 := 10; // -> noticed the range
Dt7.Name := 'T7';
Dt7.Start;
Dt8 := TGetDtThr.Create( True,@ThreadDone);
Dt8.IdLoc := 'Job5';
Dt8.Range1 := 11; // -> noticed the range
Dt8.Range2 := 20; // -> noticed the range
Dt8.Name := 'T8';
Dt8.Start;
Also, procedure threard done, now look like these :
procedure TMainFrm.ThreadDone(Sender: TObject);
begin
ThreadsRunning := ThreadsRunning-1;
lblThr.Caption:= IntToStr(ThreadsRunning);
if (ThreadsRunning <= 0) then
begin
ShowMessage('Finished');
end
else
begin
if (TGetDtThr(Sender).Name = 'Ret') then
begin
Go2();
end;
end;
end;
Procedure Go2
procedure TMainFrm.Go2();
var
Dt3 : TGetDtThr;
begin
// Loc3
Dt3 := TGetDtThr.Create( True,@ThreadDone);
Dt3.IdLoc := 'Job5';
Dt3.Range1 := 21;
Dt3.Range2 := 30;
Dt3.Name := 'T1';
Dt3.Start;
end;
Is there a guide about how many threads should use to achieve maximum performance?