Я пытаюсь осмыслить результат примера, который я кодировал.Вот оно:
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
Почему заказ не поддерживается?
Edit1:
Я добавил threadId.Кажется, что потоки постоянно переключаются.Это почему?Моя платформа - консольное приложение .NET Core в Windows.
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