事实证明我的这段代码是经得起考验的
线程池
////////////////////////////////////////////////////////////////////////////////
//
// iamy 2007-4-29
//
// 线程管理类型 对于多个线程进行管理
//
// 支持多线程并发访问
//
////////////////////////////////////////////////////////////////////////////////
unit MThreadMgr;
interface
uses
windows, classes;
type
TMThreadMgr = class(TComponent)
private
FThreadList: TThreadList;
function GetCount: Integer;
function GetItems(Index: Integer): TThread;
procedure SetItems(Index: Integer;
const Value: TThread);
protected
public
constructor Create(AOwner : TComponent);
override;
destructor Destroy;
override;
//锁定并访问线程列表 注意;谨慎使用! 对此方法的访问可能影响程序效率,
// 用完了一定要释放访问权
function LockList: TList;
//释放列表访问权
procedure UnlockList;
//添加一个线程
procedure Add(AThread: TThread);
virtual;
//删除一个线程
procedure Remove(AThread: TThread);
virtual;
//共有多少个线程
property Count: Integer read GetCount;
//根据Index取线程
property Items[Index: Integer]: TThread read GetItems write SetItems;
//根据TMediaFileUpDateThread取Index
function IndexOfThread(AThread: TThread): Integer;
//清空线程列表
// 注意:这里并没有释放实例
procedure Clear;
//全部结束
procedure TerminateAll;
//终止并释放一个线程 注意:需要子线程自己控制释放
procedure TerminateAndFree(AThread: TThread);
//全部结束并释放,执行成功返回线程数
function TerminateAndFreeAll: Integer;
//
//全部结束并释放,并等待
function TerminateAndFreeAllAndWait: Integer;
//强行终止
function FourceTerminate(AThread: TThread): Boolean;
//强行终止并释放
function FourceTerminateAndFree(AThread: TThread):Boolean;
//强行清空
procedure FourceTerminateAndFreeAll;
end;
implementation
{ TMediaFileUpdateThreadList }
procedure TMThreadMgr.Add(
AThread: TThread);
begin
Self.FThreadList.Add(AThread);
end;
constructor TMThreadMgr.Create(AOwner: TComponent);
begin
inherited;
Self.FThreadList := TThreadList.Create;
end;
destructor TMThreadMgr.Destroy;
begin
Self.TerminateAndFreeAll;
if Self.Count > 0 then
Self.FourceTerminateAndFreeAll;
Self.FThreadList.Free;
inherited;
end;
procedure TMThreadMgr.FourceTerminateAndFreeAll;
var
i : Integer;
List : TList;
begin
List := Self.FThreadList.LockList;
try
if List.Count > 0 then
begin
for i := 0 to List.Count - 1do
begin
Self.FourceTerminateAndFree(
TThread(List.Items[0])
);
end;
end;
finally
Self.FThreadList.UnlockList;
end;
end;
function TMThreadMgr.FourceTerminate(
AThread: TThread): Boolean;
begin
Result := TerminateThread(AThread.Handle,0);
end;
function TMThreadMgr.FourceTerminateAndFree(AThread: TThread): Boolean;
begin
Result := Self.FourceTerminate(AThread);
if Result then
begin
AThread.Free;
Self.FThreadList.Remove(AThread);
end;
end;
function TMThreadMgr.GetCount: Integer;
var
List : TList;
begin
List := Self.FThreadList.LockList;
try
Result := List.Count;
finally
Self.FThreadList.UnlockList;
end;
end;
function TMThreadMgr.GetItems(Index: Integer): TThread;
var
List: TList;
begin
List := Self.FThreadList.LockList;
try
Result := TThread(List.items[Index]);
finally
Self.FThreadList.UnlockList;
end;
end;
function TMThreadMgr.IndexOfThread(
AThread: TThread): Integer;
var
List : TList;
begin
List := Self.FThreadList.LockList;
try
Result := List.IndexOf(AThread);
finally
Self.FThreadList.UnlockList;
end;
end;
procedure TMThreadMgr.Remove(
AThread: TThread);
begin
Self.FThreadList.Remove(AThread);
end;
procedure TMThreadMgr.SetItems(Index: Integer;
const Value: TThread);
var
List: TList;
begin
List := Self.FThreadList.LockList;
try
List.Items[Index] := Value;
finally
Self.FThreadList.UnlockList;
end;
end;
procedure TMThreadMgr.TerminateAll;
var
i : Integer;
List : TList;
begin
List := Self.FThreadList.LockList;
try
if List.Count > 0 then
begin
for i := 0 to List.Count - 1do
begin
TThread(List.Items).Terminate;
end;
end;
finally
Self.FThreadList.UnlockList;
end;
end;
function TMThreadMgr.TerminateAndFreeAll: Integer;
var
i : Integer;
List : TList;
begin
Result := -1;
try
List := Self.FThreadList.LockList;
try
if List.Count > 0 then
begin
for i := 0 to List.Count - 1do
begin
Self.TerminateAndFree(TThread(List.Items));
end;
end;
Result := List.Count;
finally
Self.FThreadList.UnlockList;
end;
except
end;
end;
procedure TMThreadMgr.TerminateAndFree(AThread: TThread);
var
NeedFree: Boolean;
begin
NeedFree := not AThread.FreeOnTerminate;
AThread.Terminate;
if NeedFree then
AThread.Free;
//注意: 和强行终止并释放的区别
//这里为了保证删除的结果在列表中正确的体现出来
//remove操作是在子线程中实现的
end;
procedure TMThreadMgr.Clear;
begin
Self.FThreadList.Clear;
end;
function TMThreadMgr.LockList: TList;
begin
Result := Self.FThreadList.LockList;
end;
procedure TMThreadMgr.UnlockList;
begin
Self.FThreadList.UnlockList;
end;
function TMThreadMgr.TerminateAndFreeAllAndWait: Integer;
var
List : TList;
WaitedCount : Integer;
NowCount : Integer;
const
WaitTimeOutCount = 100;
begin
Result := -1;
try
NowCount := Self.TerminateAndFreeAll;
WaitedCount := 0;
while (WaitedCount < WaitTimeOutCount) and (NowCount > 0)do
begin
List := Self.FThreadList.LockList;
try
NowCount := List.Count;
finally
Self.FThreadList.UnlockList;
end;
Sleep(50);
Inc(WaitedCount);
end;
except
end;
end;
end.
线程
////////////////////////////////////////////////////////////////////////////////
//
// TMThread 提供在线程列表中的一系列操作
//
// iamy 2007.4.29
//
//
////////////////////////////////////////////////////////////////////////////////
unit MThread;
interface
uses
Classes, MThreadMgr;
type
TMThread = class(TThread)
private
FOwner: TMThreadMgr;
procedure SetOwner(const Value: TMThreadMgr);
protected
public
constructor Create(Owner: TMThreadMgr);
virtual;
destructor Destroy;
override;
property Owner : TMThreadMgr read FOwner write SetOwner;
end;
implementation
constructor TMThread.Create(Owner: TMThreadMgr);
begin
inherited Create(True);
Self.FOwner := Owner;
Self.FreeOnTerminate := True;
end;
destructor TMThread.Destroy;
begin
try
if Assigned(Self.FOwner) then
if Self.FOwner is TMThreadMgr then
TMThreadMgr(Self.FOwner).Remove(Self);
//告诉列表拥有者删除自己
finally
inherited;
end;
end;
procedure TMThread.SetOwner(const Value: TMThreadMgr);
begin
FOwner := Value;
end;
end.