【问题标题】:speed of requests请求速度
【发布时间】:2012-06-21 01:30:57
【问题描述】:

我有代码通过两个并行的每个循环发送 Web 请求。在此方法发生之前添加线程会导致这些任务的执行延迟,还是会实现更多的 Web 请求?

            for (int i = 0; i < threads; i++)
            {
                populateClient(ServerID, debugLog, type, mC);
                tasks[i] = Task.Factory.StartNew(() =>
                {
                   testMaps(ServerID, DebugLog, Type, mC.cl, mC.keywords, mC.locations);
                });
            }
            while (tasks.Any(t => !t.IsCompleted)) { } //spin wait

//...

  static void testMaps(Int64 serverID, String debugLog, String type, ClientInfo cl,
        List<Keyword> keywords,
        List<String> locations)
    {
        Parallel.ForEach(keywords, keyword =>  
        {
            Parallel.ForEach(locations, location =>
           {
           strFileName = file.DownloadString(query);

//..

【问题讨论】:

    标签: c# multithreading task parallel.foreach


    【解决方案1】:

    你的程序很好。像这样的 I/O 可以以较高的并行度并行运行,因此启动许多任务,无论是显式的还是通过Parallel.ForEach 都不是问题。该实现通常会很好地管理它们并且不会造成不必要的开销,因为它基于底层线程池。

    【讨论】:

    • 很酷,谢谢,我担心我会产生更多开销,这可能会减慢进程。
    猜你喜欢
    • 1970-01-01
    • 2017-05-22
    • 1970-01-01
    • 2016-09-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多