【问题标题】:Asynchronous and parallel-processing. Sleeping in methods异步和并行处理。睡觉的方法
【发布时间】:2019-10-28 18:51:36
【问题描述】:

假设有 10 张图片和一个团队,例如 2 人。第一个1分钟处理图像,第二个2分钟。处理需要多长时间?

我在几秒钟内创建了两个具有较低值的人。我称之为过程方法。

您需要为不同的对象并行调用两个方法。第一个比第二个工作得更快。可以使用带有图像数量的通用变量,该变量在一个或另一个流处理后会发生变化。

当然,没有任何效果。我试过.WhenAll,还是不行。

应该是 7.5 秒左右,而不是 14 秒。

主要:

using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Test.Controller;
using Test.View;
using Test.Model;

namespace Test
{
    class Program
    {
        static async Task Main(string[] args)
        {
            Person person1 = new Person(1000);
            Person person2 = new Person(2000);

            List<Person> people = new List<Person> { person1, person2 };

            int images_amount = 10;
            int time_processing = await ProcessingImages.ExecuteAsync(images_amount, people);

            TimeProcessing.TimeExecute(time_processing);
        }
    }
}

控制器:

using System;
using System.Threading;
using System.Collections.Generic;
using System.Threading.Tasks;
using Test.Model;

namespace Test.Controller
{
    class ProcessingImages
    {
        static async public Task<int> ExecuteAsync(int images_amount, List<Person> people, int time_processing = 0)
        {
            List<Task> tasks = new List<Task>();

            foreach (Person p in people)
            {
                tasks.Add(Task.Run(() => Execute(ref images_amount, p.Speed)));
            }
            await Task<int>.WhenAll(tasks);

            foreach (Task<int> task in tasks)
            {
                time_processing += await task;
            }
            return time_processing;
        }

        static private int Execute(ref int images_amount, int seconds_per_image, int count = 0)
        {
            while (images_amount > 0)
            {
                images_amount--;
                count += seconds_per_image;
                Console.WriteLine("Start thread " + seconds_per_image);
                Thread.Sleep(seconds_per_image);
            }
            return count;
        }
    }
}

查看:

using System;

namespace Test.View
{
    class TimeProcessing
    {
        static public void TimeExecute(int time_processing)
        {
            Console.WriteLine("Total time processing: {0} seconds", time_processing);
        }
    }
}

【问题讨论】:

  • 不完全清楚您的问题是什么,而且您似乎没有包含minimal reproducible example 所需的所有代码。
  • 另外,你为什么使用Task.Run 来调用Execute 而不是仅仅使该函数本身async 或返回Task
  • @Herohtar 我不知道。
  • 你的输出应该是 20 条日志 Start thread ... 因为你调用了这些任务 2 次。第一次是运行.WhenAll(tasks),第二次是遍历集合并等待每个任务再次执行。你知道吗?

标签: c# .net asynchronous parallel-processing


【解决方案1】:

你只是测量错误。您将告诉每个线程延迟的时间相加,而不是从第一个线程开始到最后一个线程完成的真实时间。

如果您使用Stopwatch 来实际计时需要多长时间,大约是 7 秒:

var watch = new Stopwatch();
watch.Start();

int time_processing = await ProcessingImages.ExecuteAsync(images_amount, people);

watch.Stop();
Console.WriteLine("Total time processing: {0}ms", watch.ElapsedMilliseconds);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-09-04
    • 1970-01-01
    • 2014-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多