当我在一个线程中下载文件时,需要 0.1 秒。但是当我在 100 个线程中下载同一个文件时 - 每次下载需要 10 秒。源代码:
private static int _threadsCount;
private static string _url;
private static void Main(string[] args)
{
    _url = ConfigurationManager.AppSettings["Url"];
    int threadsLimit = 1;
    if (0 != args.Length)
        threadsLimit = int.Parse(args[0]);
    for (int i = 0; i < threadsLimit; i++)
    {
        var thread = new Thread(Start);
        thread.Start();
    }
    while (_threadsCount < threadsLimit)
    {
        Thread.Sleep(1000);
    }
    Console.WriteLine("Done");
}
static void Start()
{
    var webClient = new WebClient();
    var stopwatch = new Stopwatch();
    stopwatch.Reset();
    stopwatch.Start();
    for (int i = 1; i <= 10; i++)
    {
        webClient.DownloadData(_url);
    }
    stopwatch.Stop();
    Console.WriteLine(stopwatch.ElapsedMilliseconds);
    Interlocked.Increment(ref _threadsCount);
}
因此,如果我运行一个有 100 个线程的程序,每个文件的速度为 10 秒。但是如果我用 1 个线程同时运行第二个程序,每个文件的速度为 0.1 秒。所以,问题不在于网速。
为什么下载速度随着线程数的增加而下降,但不影响其他进程(同一个文件)?如何提高一个进程的速度?