本文整理自网络,侵删。
TParallel创建的任务/线程或多或少是持久性的,这可能有利于重用。但是,我希望看到一个用于清理的类方法。
TParallel.For(0, 20, (procedure(A : integer) begin writeln(A) end));
uses
System.Diagnostics,
System.Threading;
上面的代码留下了线程,可以在任务管理器中观察到。
创建自己的TThreadPool实例并在不再需要时处置它会令您满意吗?
var
Pool: TThreadPool;
begin
Pool := TThreadPool.Create;
try
TParallel.For(0, 20, (procedure(A : integer) begin writeln(A) end), Pool);
Write('Disposing of the thread pool - press ENTER to continue...');
finally
Pool.DisposeOf;
end;
end;
线程池仍然存在,可以有效地重用。当需要显式管理线程时,建议使用TThreadpool。
相关阅读 >>
Delphi关于tjpegimage的使用(bmp\ jpg格式转换)
更多相关阅读请进入《Delphi》频道 >>