static void Main() { Foo(); Console.ReadLine(); } private static async Task Foo() { var urls = new[] { "A", "B", "C", "D", "E", }; var maxThreads = 4; var q = new ConcurrentQueue<string>(urls); var tasks = new List<Task>(); for (int n = 0; n < maxThreads; n++) { tasks.Add(Task.Run(async () => { while (q.TryDequeue(out string url)) { Console.WriteLine($"start {url}"); await Task.Delay(3000); Console.WriteLine($"end {url}"); } })); } await Task.WhenAll(tasks); }ref: sound code
沒有留言:
張貼留言