gpt4 book ai didi

c# - 异步/等待超出预期顺序

转载 作者:太空宇宙 更新时间:2023-11-03 22:44:42 25 4
gpt4 key购买 nike

我正在努力了解我编写的一个示例的结果。在这里:

    class Program
{
static async Task Main(string[] args)
{
var counter1 = new Counter("counter1",string.Empty);
var counter2 = new Counter("counter2"," ");
var counter3 = new Counter("counter3"," ");

await Task.WhenAll(counter1.Count(), counter2.Count(),
counter3.Count());
}
}

public class Counter
{
private string _name;
private string _prefix;

public Counter(string name, string prefix)
{
_name = name;
_prefix = prefix;
}

public async Task Count()
{
for (int i = 0; i <= 10; i++)
{
Console.Write($"{_prefix}{_name}: {i}");
if (i % 2 == 0 && i != 0)
{
Console.WriteLine($" is divisible by 2. Yielding");
await Task.Yield();
}
else
{
Console.WriteLine();
}
}
}
}

我希望计数器产生一个输出,其中它们在 2 行后切换。相反,从 counter2 开始,当它数到 5 时,它就像在自行屈服。我得到的是以下内容:

counter1: 0
counter1: 1
counter1: 2 is divisible by 2. Yielding
counter2: 0
counter2: 1
counter2: 2 is divisible by 2. Yielding
counter3: 0
counter3: 1
counter3: 2 is divisible by 2. Yielding
counter1: 3
counter1: 4 is divisible by 2. Yielding
counter2: 3
counter2: 4 is divisible by 2. Yielding
counter1: 5
counter1: 6 is divisible by 2. Yielding
counter2: 5
counter3: 3
counter1: 7
counter1: 8 is divisible by 2. Yielding
counter2: 6 is divisible by 2. Yielding
counter3: 4 is divisible by 2. Yielding
counter2: 7
counter2: 8 is divisible by 2. Yielding
counter3: 5
counter3: 6 is divisible by 2. Yielding
counter3: 7
counter3: 8 is divisible by 2. Yielding
counter1: 9
counter3: 9
counter3: 10 is divisible by 2. Yielding
counter1: 10 is divisible by 2. Yielding
counter2: 9
counter2: 10 is divisible by 2. Yielding

为什么没有维护订单?

编辑1:

我已经添加了 threadId。似乎线程在不断切换。这是为什么?我的平台是 Windows 上的 .NET Core 控制台应用程序。

counter1: 0. ThreadId: 1
counter1: 1. ThreadId: 1
counter1: 2. ThreadId: 1 is divisible by 2. Yielding
counter2: 0. ThreadId: 1
counter2: 1. ThreadId: 1
counter2: 2. ThreadId: 1 is divisible by 2. Yielding
counter3: 0. ThreadId: 1
counter3: 1. ThreadId: 1
counter3: 2. ThreadId: 1 is divisible by 2. Yielding
counter1: 3. ThreadId: 3
counter1: 4. ThreadId: 3 is divisible by 2. Yielding
counter3: 3. ThreadId: 4
counter3: 4. ThreadId: 4 is divisible by 2. Yielding
counter3: 5. ThreadId: 4
counter3: 6. ThreadId: 4 is divisible by 2. Yielding
counter1: 5. ThreadId: 3
counter1: 6. ThreadId: 3 is divisible by 2. Yielding
counter3: 7. ThreadId: 4
counter3: 8. ThreadId: 4 is divisible by 2. Yielding
counter3: 9. ThreadId: 4
counter3: 10. ThreadId: 4 is divisible by 2. Yielding
counter1: 7. ThreadId: 3
counter1: 8. ThreadId: 3 is divisible by 2. Yielding
counter1: 9. ThreadId: 3
counter1: 10. ThreadId: 3 is divisible by 2. Yielding
counter2: 3. ThreadId: 5
counter2: 4. ThreadId: 5 is divisible by 2. Yielding
counter2: 5. ThreadId: 5
counter2: 6. ThreadId: 5 is divisible by 2. Yielding
counter2: 7. ThreadId: 5
counter2: 8. ThreadId: 5 is divisible by 2. Yielding
counter2: 9. ThreadId: 5
counter2: 10. ThreadId: 5 is divisible by 2. Yielding

最佳答案

您在控制台应用程序中,因此默认情况下没有同步上下文。在第一个 await 运行后,它的继续将被安排在另一个线程池线程上。在主线程上,第二个计数器开始,当它再次到达 await 时,它会产生另一个“分支”。然后它们将并行运行,而您将失去预期的顺序。

如果您希望一次只运行一个任务,则需要使用同步上下文或只调度一个线程的任务调度器。

关于c# - 异步/等待超出预期顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50551017/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com