我想使用httpwebrequest
访问Web服务器,并从给定范围的页面中获取数千条记录。每击中一个网页就会获取15条记录,并且网络服务器上几乎有8到10000个页面。这意味着服务器总共有120000次点击!如果使用单个进程轻松完成,任务可能非常耗时。因此,多线程是想到的即时解决方案。使用多个线程来实现从Web服务器进行高效搜索
目前,我创建了一个用于搜索目的的工作者类,该工作者类将产生5个子工作者(线程)来在给定范围内进行搜索。但是,由于我的线程新手能力,我无法使其工作,因为我无法同步并使它们一起工作。我知道在.NET代表,行动,事件,而是使其与螺纹越来越confusing..This工作是我使用的代码:
public void Start()
{
this.totalRangePerThread = ((this.endRange - this.startRange)/this.subWorkerThreads.Length);
for (int i = 0; i < this.subWorkerThreads.Length; ++i)
{
//theThreads[counter] = new Thread(new ThreadStart(MethodName));
this.subWorkerThreads[i] = new Thread(() => searchItem(this.startRange, this.totalRangePerThread));
//this.subWorkerThreads[i].Start();
this.startRange = this.startRange + this.totalRangePerThread;
}
for (int threadIndex = 0; threadIndex < this.subWorkerThreads.Length; ++threadIndex)
this.subWorkerThreads[threadIndex].Start();
}
的searchItem方法:
public void searchItem(int start, int pagesToSearchPerThread)
{
for (int count = 0; count < pagesToSearchPerThread; ++count)
{
//searching routine here
}
}
问题存在于线程的共享变量之间,任何人都可以引导我如何使它成为一个线程安全的过程?
我编辑了你的标题。请参阅:“[应该在其标题中包含”标签“](http://meta.stackexchange.com/questions/19190/)”,其中的共识是“不,他们不应该”。 – 2013-03-22 18:46:42
@JohnSaunders下次会小心..谢谢 – faizanjehangir 2013-03-22 18:49:57
@faizanjehangir - 您是否考虑过使用提供FTS功能的数据库?如果您正在手动执行操作,这对DDOS非常敏感。 – tjameson 2013-03-22 18:53:12