[英]Parallel Programming C# - TPL - Update Shared Variable & Suggestions
I am new to Parallel Programming and infact this is the first time I am trying it. 我是并行编程的新手,实际上这是我第一次尝试。 I am currently doing a project in .NET 4 and prefer to do have 4 or 5 parallel executions. 我目前正在.NET 4中做一个项目,并且希望有4或5个并行执行。
I see some options. 我看到一些选择。 There is Task.Factory.StartNew
Parallel.For
Parallel.ForEach
etc. 有Task.Factory.StartNew
Parallel.For
Parallel.ForEach
等。
What I am going to do is post to a web-site and fetch the responses for about 200 URLs. 我要做的是发布到网站上并获取大约200个URL的响应。
When I use Parallel.ForEach
I didn't find a way to control the number of threads and the application went using 130+ threads and the website went unresponsive :) 当我使用Parallel.ForEach
我找不到控制线程数的方法,并且应用程序使用了130多个线程,并且网站无响应:)
I am interested in using Task.Factory.StartNew
within a for loop and divide the URLs in to 4 or 5 tasks. 我对在for循环中使用Task.Factory.StartNew
感兴趣,并将URL分为4个或5个任务。
List<Task> tasks = new List<Task>();
for (int i = 0; i < 5; i++)
{
List<string> UrlForTask = GetUrlsForTask(i,5); //Lets say will return some thing like 1 of 5 of the list of URLs
int j = i;
var t = Task.Factory.StartNew(() =>
{
List<PageSummary> t = GetSummary(UrlForTask);
Summary.AddRange(t); //Summary is a public variable
}
tasks.Add(t);
}
I believe that these Tasks kind of boil down to threads. 我相信这些任务可以归结为线程。 So if I make Summary
a List<PageSummary>
will it be kind of thread safe (I understand there are issues accessing a shared variable by multiple threads)? 因此,如果我将Summary
为List<PageSummary>
它是否是线程安全的(我知道多个线程访问共享变量存在问题)?
Is this where we should use ConcurrentQueue<T>
? 这是我们应该使用ConcurrentQueue<T>
吗?
Do you know of a good resource that helps to learn about accessing and updating a shared variable by multiple tasks etc? 您是否知道一个好的资源,可以帮助您了解通过多个任务等访问和更新共享变量?
What is the best way I could use for this type of task as you may think ? 您可能会想到,我可以用于此类任务的最佳方法是什么?
Parallel.ForEach
has overloads that take a ParallelOptions
instance. Parallel.ForEach
具有采用ParallelOptions
实例的重载。 The MaxDegreeOfParallelism
property of that class is what you need to use. MaxDegreeOfParallelism
属性是您需要使用的属性。
List<MyRequest> requests = ...;
BlockingCollection<MyResponse> responses = ...;
Task.Factory.StartNew(() =>
{
Parallel.ForEach(
requests,
new ParallelOptions { MaxDegreeOfParallelism = 4 },
request => responses.Add(MyDownload(request)));
responses.CompleteAdding();
});
foreach (var response in responses.GetConsumingEnumerable())
{
Console.WriteLine(response.MyMessage);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.