多线程 – 提高性能的线程
发布时间:2020-12-14 05:57:59 所属栏目:Java 来源:网络整理
导读:我从未使用过线程 – 从未想过我的代码会受益.但是,我认为线程可能会提高以下伪代码的性能: Loop through table of records containing security symbol field and a quote field Load a web page (containing a security quote for a symbol) into a strin
|
我从未使用过线程 – 从未想过我的代码会受益.但是,我认为线程可能会提高以下伪代码的性能:
Loop through table of records containing security symbol field and a quote field
Load a web page (containing a security quote for a symbol) into a string variable
Parse the string for the quote
Save the quote in the table
Get next record
end loop
加载每个网页需要花费最多的时间.解析报价非常快.我想我可以拿一个线程的一半记录,然后在另一个线程中处理另一半. 解决方法
在
OmniThreadLibrary中,使用多级管道解决此问题非常简单 – 第一阶段在多个任务上运行并在一个实例中下载网页和第二阶段运行并将数据存储到数据库中.我前段时间写了一个记录这个解决方案的
blog post.
解决方案可以用以下代码总结(您必须在HttpGet和Inserter方法中填写一些地方). uses
OtlCommon,OtlCollections,OtlParallel;
function HttpGet(url: string; var page: string): boolean;
begin
// retrieve page contents from the url; return False if page is not accessible
end;
procedure Retriever(const input: TOmniValue; var output: TOmniValue);
var
pageContents: string;
begin
if HttpGet(input.AsString,pageContents) then
output := TPage.Create(input.AsString,pageContents);
end;
procedure Inserter(const input,output: IOmniBlockingCollection);
var
page : TOmniValue;
pageObj: TPage;
begin
// connect to database
for page in input do begin
pageObj := TPage(page.AsObject);
// insert pageObj into database
FreeAndNil(pageObj);
end;
// close database connection
end;
procedure ParallelWebRetriever;
var
pipeline: IOmniPipeline;
s : string;
urlList : TStringList;
begin
// set up pipeline
pipeline := Parallel.Pipeline
.Stage(Retriever).NumTasks(Environment.Process.Affinity.Count * 2)
.Stage(Inserter)
.Run;
// insert URLs to be retrieved
for s in urlList do
pipeline.Input.Add(s);
pipeline.Input.CompleteAdding;
// wait for pipeline to complete
pipeline.WaitFor(INFINITE);
end;
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
