delphi Delphi TParallel Cleanup Needed用法  
官方Delphi 学习QQ群: 682628230(三千人)
频道

delphi Delphi TParallel Cleanup Needed用法


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。


推荐分享
图文皆来源于网络,内容仅做公益性分享,版权归原作者所有,如有侵权请告知删除!
 

Copyright © 2014 DelphiW.com 开发 源码 文档 技巧 All Rights Reserved
晋ICP备14006235号-8 晋公网安备 14108102000087号

执行时间: 0.036970853805542 seconds