简体   繁体   English

为什么这些任务按顺序执行?

[英]Why do these tasks execute sequentially?

Why does the following code execute sequentially? 为什么以下代码按顺序执行?

List<Task> tasks = new List<Task>();

for (int i = 0; i <= max; i += block)
{
    if (i + block >= max)
        tasks.Add(Task.Factory.StartNew(() => Count(ref counter, block)));
    else
        block = max - i;
}

Task.WaitAll(tasks.ToArray());

I have also tested a version of this using Parallel.Invoke ; 我还使用Parallel.Invoke测试了它的一个版本; it, too, fails to execute in parallel. 它也不能并行执行。 There's bound to be something I'm not understanding, but when I try Googling this, I mostly get instructions on how to force sequential execution. 肯定会有一些我不理解的东西,但是当我尝试谷歌搜索时,我主要得到关于如何强制顺序执行的说明。

As a response to one of the caveats given in an answer below, I have included the following method for reference: 作为对以下答案中给出的警告之一的回应,我已经包括以下方法供参考:

static void Count(ref int counter, int num)
{
    int localCounter = 0;
    for (int i = 0; i < num; i++)
        if (Coin()) localCounter++;
    System.Threading.Interlocked.Add(ref counter, localCounter);
}

Edited again: Thank you all! 再次编辑:谢谢大家!

Just replace tasks.Add(Task.Factory.StartNew(() => Count(ref counter, block))); 只需替换tasks.Add(Task.Factory.StartNew(() => Count(ref counter, block))); with Console.WriteLine and debug your code. 使用Console.WriteLine并调试代码。

You never create more than one task. 您永远不会创建多个任务。

for (int i = 0; i <= max; i += block)
{
    if (i + block >= max)
        Console.WriteLine(i);
    else
        block = max - i;

}

why does the following code execute sequentially? 为什么以下代码按顺序执行?

It doesn't, unless you have something within the Count method that's synchronizing access to a single resource. 它不会,除非您在Count方法中有某些内容正在同步对单个资源的访问。 If it can be parallelized, then this will run in parallel. 如果它可以并行化,那么这将并行运行。

If Count executes very quickly then you'll find that the tasks are finished faster than new tasks get scheduled, which is why they may all execute in order. 如果Count执行得非常快,那么你会发现任务的完成速度比新任务的安排要快,这就是为什么它们都可以按顺序执行。

Seems to me there is something wrong with your loop/if statement: 在我看来你的loop / if语句有问题:

for (int i = 0; i <= max; i += block)
{
    if (i + block >= max)
        tasks.Add(Task.Factory.StartNew(() => Count(ref counter, block)));
    else
        block = max - i;
}

If I am reading it right, you will only add a task if i + block >= max and you will only loop if i + block <= max (showing both the counter increase and the condition check). 如果我正确读取它,你只会在i + block >= max添加一个任务,并且只有当i + block <= max时才会循环(显示计数器增加和条件检查)。 As such you will only add a task once. 因此,您只需添加一次任务。

In addition, you are changing block when you are not adding a task. 此外,当您不添加任务时,您正在更改阻止。 I expect you want something more like the following, though I can not be sure without more code: 我希望你想要更像下面这样的东西,但我不能确定没有更多的代码:

for (int i = 0; i <= max; i += block)
{
    tasks.Add(Task.Factory.StartNew(() => Count(ref counter, block)));
    if (i + block >= max) { block = max - i; }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM