using System; using System.Collections.Generic; using System.Threading; using System.Threading.Tasks; class Async { public class Number { public int Id; public int Num; } public static async Task Main(string[] args) { List nums = new List(); //One would expect that the following code runs truly asynchronously, on the same thread. //However: Task task1 = FooAsync(1, nums); //This runs on one thread Task task2 = FooAsync(2, nums); //This runs on another thread await task1; await task2; foreach(var num in nums) { Console.WriteLine($"{num.Num} (Id: {num.Id})"); } } public static async Task FooAsync(int id, List nums) { Console.WriteLine($"Starting async task (id: {id}) in Thread {Thread.CurrentThread.ManagedThreadId}"); await Task.Delay(1000); for (int i = 0; i < 100; i++) { nums.Add(new Number() { Id = id, Num = i }); } Console.WriteLine($"Finished async task (id: {id}) in Thread {Thread.CurrentThread.ManagedThreadId}"); } }