indy 10有乙個元件叫idschedulerofthreadpool,網上沒有其相關使用**,好在有源**,於是自己看。其實用起來也還比較簡單,主要是實現了維護乙個執行緒池的功能,功能不是很強。不過基於該元件的開發,也可以為我們省了不少**,增強了些功能。
與該執行緒池使用相關的類主要有:
tidschedulerofthreadpool 這個不用說
tidtask(in idtask.pas) 需要使用者繼承實現的類,實現引數的傳入,及具體的run、beforerun、afterrun功能**
tidyarnofthread(in idschedulerofthread.pas) 需要強制轉換的類,只有強制轉換後才能訪問到執行緒thread物件,使用synchronize來訪問vcl控制項
------------------------ unit.pas ----------------------
unit unit1;
inte***ce
uses
windows, messages, sysutils, variants, classes, graphics, controls, forms,
dialogs, stdctrls, extctrls, idschedulerofthreadpool, idyarn,
idscheduler, idbasecomponent, idschedulerofthread, spin;
type
tform1 = class(tform)
memo1: tmemo;
idschedulerofthreadpool1: tidschedulerofthreadpool;
button4: tbutton;
button2: tbutton;
spinedit1: tspinedit;
button3: tbutton;
procedure button4click(sender: tobject);
procedure button3click(sender: tobject);
procedure button2click(sender: tobject);
procedure formcreate(sender: tobject);
private
public
end;
varform1: tform1;
astr: string;
implementation
uses
unituploadtask;
procedure tform1.button4click(sender: tobject); //執行緒池新增執行緒
varmytask: tuploadtask;
idyarn: tidyarn;
obj: uploadparamobj;
begin
obj := uploadparamobj.create;
obj.str := spinedit1.text;
idyarn := idschedulerofthreadpool1.acquireyarn;
mytask := tuploadtask.create(idyarn, obj);
idschedulerofthreadpool1.startyarn(idyarn, mytask);
end;
procedure tform1.button3click(sender: tobject); //執行緒池結束執行緒
begin
idschedulerofthreadpool1.terminateallyarns;
end;
procedure tform1.button2click(sender: tobject); //檢視活動執行緒數
begin
with idschedulerofthreadpool1.activeyarns.locklist do try
showmessage(inttostr(count));
finally
idschedulerofthreadpool1.activeyarns.unlocklist;
end;
end;
procedure tform1.formcreate(sender: tobject); //初始化執行緒池
begin
idschedulerofthreadpool1.init;
end;
end.
------------------------------------- unituploadtask.pas ---------------------------------
unit unituploadtask;
inte***ce
uses
classes, sysutils, idtask, idyarn;
type
uploadparamobj = class(tobject)
public
str: string;
end;
tuploadtask = class(tidtask)
private
thread: tthread;
trytime: integer;
procedure output;
protected
function run: boolean; override;
procedure beforerun; override;
procedure afterrun; override;
public
constructor create(ayarn: tidyarn; obj: uploadparamobj);
end;
implementation
uses
unit1, idschedulerofthread;
constructor tuploadtask.create(ayarn: tidyarn; obj: uploadparamobj);
begin
fdata := obj;
trytime := 3;
thread := tidyarnofthread(ayarn).thread;
inherited create(ayarn);
end;
function tuploadtask.run: boolean;
begin
thread.synchronize(thread, output);
sleep(5000);
if trytime > 0 then
begin
dec(trytime);
result := true;
endelse
result := false;
end;
procedure tuploadtask.beforerun;
begin
form1.memo1.lines.add('beforerun' + uploadparamobj(fdata).str);
end;
procedure tuploadtask.afterrun;
begin
form1.memo1.lines.add('afterrun' + uploadparamobj(fdata).str);
end;
procedure tuploadtask.output;
begin
form1.memo1.lines.add('上傳開始,str=' + uploadparamobj(fdata).str + ', try=' + inttostr(trytime) +',id=' + inttostr(thread.threadid));
end;
end.
操作你會發現,執行緒池是用丟擲異常來實現,因此你需要捕獲異常來進行執行緒池滿的處理。
乙個使用執行緒池的範例(翻譯)
翻譯by lwkl a programming model to use a thread pool by sherwoodhu 許多場合我們需要利用多執行緒來提高我們系統的行為。每個線程式幾乎相同,但我們必須管理這些執行緒。如果當系統繁忙了,那麼我們將建立更多的線程式,不然我們殺掉一寫來避免額外的...
10 執行緒池 執行緒排程
執行緒池 第四種 獲取執行緒的方法 執行緒池 乙個executorservice,它使用執行緒池的可能的某個執行緒之一執行每個提交的任務,通常使用 executors 工廠方法配置 executorservice service executors.newfixedthreadpool 5 執行緒池...
pthread執行緒使用範例
使用pthread執行緒庫時,應該包含標頭檔案pthread.h include編譯時需要加上 lpthread鏈結該庫 建立執行緒使用介面 int pthread create pthread t tidp,const pthread attr t attr,void start rtn void...