一次最多限制 3 个线程
本文关键字:线程 一次 | 更新日期: 2023-09-27 18:35:40
我有以下代码从各种网站下载html字符串。
class Program
{
static void Main(string[] args)
{
List<string> URLsToProcess = new List<string>()
{
"http://www.microsoft.com",
"http://www.stackoverflow.com",
"http://www.google.com",
"http://www.apple.com",
"http://www.ebay.com",
"http://www.oracle.com",
"http://www.gmail.com",
"http://www.amazon.com",
"http://www.yahoo.com",
"http://www.msn.com"
};
List<Task<string>> tURLs = new List<Task<string>>();
foreach (string url in URLsToProcess)
{
Task<string> t = DownloadStringAsTask(new Uri(url));
tURLs.Add(t);
}
Console.WriteLine("waiting now");
Task.WaitAll(tURLs.ToArray());
Console.WriteLine("download all done");
foreach (Task<string> t in tURLs)
Console.WriteLine(t.Result);
}
static Task<string> DownloadStringAsTask(Uri address)
{
TaskCompletionSource<string> tcs =
new TaskCompletionSource<string>();
WebClient client = new WebClient();
client.DownloadStringCompleted += (sender, args) =>
{
if (args.Error != null)
tcs.SetException(args.Error);
else if (args.Cancelled)
tcs.SetCanceled();
else
tcs.SetResult(args.Result);
};
client.DownloadStringAsync(address);
return tcs.Task;
}
}
使用此代码,可以启动 10 个不同的线程以从所有 10 个不同的 URL 下载数据。我想一次将其限制为最多 3 个线程。任何其他请求都应等到其中一个线程被释放。关于如何添加此限制的任何想法?
请查看数据并行性与任务并行性。您可以在此处使用并行 LINQ:
var URLsToProcess = new List<string>
{
"http://www.microsoft.com",
"http://www.stackoverflow.com",
"http://www.google.com",
"http://www.apple.com",
"http://www.ebay.com",
"http://www.oracle.com",
"http://www.gmail.com",
"http://www.amazon.com",
"http://www.yahoo.com",
"http://www.msn.com"
};
Console.WriteLine("waiting now");
string[] tURLs = URLsToProcess
.AsParallel()
.WithDegreeOfParallelism(3)
.Select(uri => this.DownloadStringAsTask(new Uri(uri)).Result)
.ToArray();
Console.WriteLine("download all done");
foreach (string t in tURLs)
{
Console.WriteLine(t);
}
您可以使用 SemaphoreSlim 类来限制一次执行的线程数。 班级课程 {
static void Main(string[] args)
{
List<string> URLsToProcess = new List<string>()
{
"http://www.microsoft.com",
"http://www.stackoverflow.com",
"http://www.google.com",
"http://www.apple.com",
"http://www.ebay.com",
"http://www.oracle.com",
"http://www.gmail.com",
"http://www.amazon.com",
"http://www.yahoo.com",
"http://www.msn.com"
};
SemaphoreSlim ss = new SemaphoreSlim(3); //limit 3 threads at a time
List<Task<string>> tURLs = new List<Task<string>>();
foreach (string url in URLsToProcess)
{
//Task<string> t = DownloadStringAsTask(new Uri(url));
//tURLs.Add(t);
tURLs.Add((Task<string>)Task.Run(() =>
{
DownloadStringAsTask(new Uri(url));
ss.Release();
}));
}
Console.WriteLine("waiting now");
Task.WaitAll(tURLs.ToArray());
Console.WriteLine("download all done");
foreach (Task<string> t in tURLs)
Console.WriteLine(t.Result);
Console.ReadLine();
}
static Task<string> DownloadStringAsTask(Uri address)
{
TaskCompletionSource<string> tcs =
new TaskCompletionSource<string>();
WebClient client = new WebClient();
client.DownloadStringCompleted += (sender, args) =>
{
if (args.Error != null)
tcs.SetException(args.Error);
else if (args.Cancelled)
tcs.SetCanceled();
else
tcs.SetResult(args.Result);
};
client.DownloadStringAsync(address);
return tcs.Task;
}
}