我正在努力弄清我编写的示例结果。这里是:

    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时,它将生成另一个“分支”。这些将并行运行,您将失去预期的订单。

如果一次只运行一个任务,则需要使用同步上下文或仅计划一个线程的任务计划程序。

09-16 11:31